Resolve the path of a module like require.resolve() but from a given path
npx @tessl/cli install tessl/npm-resolve-from@5.0.00
# Resolve From
1
2
Resolve From provides a utility function for resolving module paths from a specific directory, similar to Node.js's built-in `require.resolve()` function but with the ability to specify the starting directory. This is particularly valuable for build tools, module bundlers, test frameworks, and other development utilities that need to work with module resolution across different project structures.
3
4
## Package Information
5
6
- **Package Name**: resolve-from
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install resolve-from`
10
11
## Core Imports
12
13
```javascript
14
const resolveFrom = require("resolve-from");
15
```
16
17
For ES modules (TypeScript/modern JavaScript):
18
19
```typescript
20
import resolveFrom = require("resolve-from");
21
// or
22
import * as resolveFrom from "resolve-from";
23
```
24
25
## Basic Usage
26
27
```javascript
28
const resolveFrom = require("resolve-from");
29
30
// There is a file at `./foo/bar.js`
31
const modulePath = resolveFrom("foo", "./bar");
32
//=> '/Users/sindresorhus/dev/test/foo/bar.js'
33
34
// Silent mode - returns undefined instead of throwing
35
const maybeModulePath = resolveFrom.silent("foo", "./nonexistent");
36
//=> undefined
37
```
38
39
## Capabilities
40
41
### Module Resolution
42
43
Resolves the path of a module from a given directory. Throws an error when the module can't be found.
44
45
```typescript { .api }
46
/**
47
* Resolve the path of a module like require.resolve() but from a given path
48
* @param fromDirectory - Directory to resolve from
49
* @param moduleId - What you would use in require()
50
* @returns Resolved module path
51
* @throws TypeError when arguments are not strings
52
* @throws Error with code 'MODULE_NOT_FOUND' when module can't be found
53
*/
54
function resolveFrom(fromDirectory: string, moduleId: string): string;
55
```
56
57
**Usage Examples:**
58
59
```javascript
60
const resolveFrom = require("resolve-from");
61
62
// Resolve relative paths
63
resolveFrom("./src", "./utils");
64
//=> '/path/to/project/src/utils.js'
65
66
// Resolve npm packages
67
resolveFrom("./src", "lodash");
68
//=> '/path/to/project/node_modules/lodash/index.js'
69
70
// Create a partial function for repeated use
71
const resolveFromSrc = resolveFrom.bind(null, "./src");
72
resolveFromSrc("./config");
73
resolveFromSrc("./helpers");
74
```
75
76
### Silent Module Resolution
77
78
Resolves the path of a module from a given directory, returning `undefined` instead of throwing when the module can't be found.
79
80
```typescript { .api }
81
/**
82
* Resolve the path of a module like require.resolve() but from a given path
83
* @param fromDirectory - Directory to resolve from
84
* @param moduleId - What you would use in require()
85
* @returns Resolved module path or undefined when the module can't be found
86
*/
87
function silent(fromDirectory: string, moduleId: string): string | undefined;
88
```
89
90
**Usage Examples:**
91
92
```javascript
93
const resolveFrom = require("resolve-from");
94
95
// Safe resolution that won't throw
96
const maybeExists = resolveFrom.silent("./src", "./optional-config");
97
if (maybeExists) {
98
// Module exists, use it
99
const config = require(maybeExists);
100
}
101
102
// Create a silent partial function
103
const silentResolveFromSrc = resolveFrom.silent.bind(null, "./src");
104
const utilPath = silentResolveFromSrc("./utils");
105
```
106
107
## Error Handling
108
109
The main `resolveFrom` function throws specific errors for different failure cases:
110
111
- **TypeError**: When `fromDirectory` or `moduleId` parameters are not strings
112
- **Error with code 'MODULE_NOT_FOUND'**: When the specified module cannot be found or resolved
113
- **File system errors**: When the `fromDirectory` doesn't exist and cannot be resolved
114
115
The `silent` variant never throws these errors and returns `undefined` instead.
116
117
## Advanced Usage Patterns
118
119
### Partial Application
120
121
Create reusable resolvers for specific directories:
122
123
```javascript
124
const resolveFrom = require("resolve-from");
125
126
// Standard resolver
127
const resolveFromBuild = resolveFrom.bind(null, "./build");
128
const bundlePath = resolveFromBuild("./bundle.js");
129
130
// Silent resolver
131
const silentResolveFromSrc = resolveFrom.silent.bind(null, "./src");
132
const optionalPath = silentResolveFromSrc("./optional-module");
133
```
134
135
### Error Handling Patterns
136
137
```javascript
138
const resolveFrom = require("resolve-from");
139
140
try {
141
const modulePath = resolveFrom("./src", "some-module");
142
console.log("Module found at:", modulePath);
143
} catch (error) {
144
if (error.code === 'MODULE_NOT_FOUND') {
145
console.log("Module not found");
146
} else if (error instanceof TypeError) {
147
console.log("Invalid arguments provided");
148
} else {
149
console.log("Other error:", error.message);
150
}
151
}
152
```
153
154
## Implementation Notes
155
156
- Uses Node.js built-in modules: `path`, `Module`, and `fs`
157
- Zero runtime dependencies
158
- Handles symlinks through `fs.realpathSync()`
159
- Works with both relative and absolute module paths
160
- Compatible with Node.js >= 8
161
- Follows Node.js module resolution algorithm via `Module._resolveFilename()`