Resolve the path of a module like require.resolve() but from the current working directory
npx @tessl/cli install tessl/npm-resolve-cwd@3.0.00
# Resolve CWD
1
2
Resolve CWD provides a simple utility for resolving module paths from the current working directory. It wraps Node.js's module resolution system to resolve modules relative to `process.cwd()` instead of the calling file's location, making it useful for CLI tools, build systems, and other utilities that need to resolve modules from a specific working context.
3
4
## Package Information
5
6
- **Package Name**: resolve-cwd
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install resolve-cwd`
10
11
## Core Imports
12
13
```javascript
14
const resolveCwd = require('resolve-cwd');
15
```
16
17
For ESM:
18
19
```javascript
20
import resolveCwd from 'resolve-cwd';
21
```
22
23
For TypeScript:
24
25
```typescript
26
import resolveCwd = require('resolve-cwd');
27
```
28
29
## Basic Usage
30
31
```javascript
32
const resolveCwd = require('resolve-cwd');
33
34
console.log(__dirname);
35
//=> '/Users/sindresorhus/rainbow'
36
37
console.log(process.cwd());
38
//=> '/Users/sindresorhus/unicorn'
39
40
console.log(resolveCwd('./foo'));
41
//=> '/Users/sindresorhus/unicorn/foo.js'
42
43
// Silent variant - returns undefined instead of throwing
44
console.log(resolveCwd.silent('./nonexistent'));
45
//=> undefined
46
```
47
48
## Capabilities
49
50
### Module Resolution
51
52
Resolves the path of a module from the current working directory, similar to `require.resolve()` but using `process.cwd()` as the base directory.
53
54
```javascript { .api }
55
/**
56
* Resolve the path of a module from the current working directory
57
* @param moduleId - What you would use in require()
58
* @returns The resolved module path
59
* @throws When the module can't be found
60
*/
61
resolveCwd(moduleId: string): string;
62
```
63
64
**Usage Examples:**
65
66
```javascript
67
const resolveCwd = require('resolve-cwd');
68
69
// Resolve relative path from current working directory
70
const localModule = resolveCwd('./lib/utils');
71
//=> '/current/working/dir/lib/utils.js'
72
73
// Resolve npm package from current working directory's node_modules
74
const packagePath = resolveCwd('lodash');
75
//=> '/current/working/dir/node_modules/lodash/index.js'
76
77
// Resolve with file extension
78
const jsFile = resolveCwd('./config.json');
79
//=> '/current/working/dir/config.json'
80
```
81
82
### Silent Module Resolution
83
84
Resolves the path of a module from the current working directory without throwing errors. Returns `undefined` when the module cannot be found.
85
86
```javascript { .api }
87
/**
88
* Resolve the path of a module from the current working directory (silent)
89
* @param moduleId - What you would use in require()
90
* @returns The resolved module path or undefined if not found
91
*/
92
resolveCwd.silent(moduleId: string): string | undefined;
93
```
94
95
**Usage Examples:**
96
97
```javascript
98
const resolveCwd = require('resolve-cwd');
99
100
// Safe resolution - no error thrown
101
const existingModule = resolveCwd.silent('./existing-file');
102
//=> '/current/working/dir/existing-file.js'
103
104
const nonExistentModule = resolveCwd.silent('./nonexistent');
105
//=> undefined
106
107
// Useful for optional dependencies
108
const optionalConfig = resolveCwd.silent('./optional-config.js');
109
if (optionalConfig) {
110
// Load optional configuration
111
const config = require(optionalConfig);
112
}
113
```
114
115
## Error Handling
116
117
The main `resolveCwd()` function throws standard Node.js module resolution errors when a module cannot be found:
118
119
- **MODULE_NOT_FOUND**: When the specified module doesn't exist
120
- **INVALID_MODULE_PATH**: When the module path is invalid
121
122
The `resolveCwd.silent()` variant never throws and returns `undefined` for any resolution failures.
123
124
## Use Cases
125
126
- **CLI Tools**: Resolve configuration files or plugins from the user's current directory
127
- **Build Systems**: Resolve source files relative to the project root
128
- **Development Tools**: Load project-specific modules from the current working context
129
- **Testing Utilities**: Resolve test files or fixtures from the test runner's working directory