0
# Core Module Detection
1
2
Utilities for detecting and working with Node.js core modules during module resolution. These functions help determine whether a given module name refers to a built-in Node.js module like 'fs', 'path', or 'http'.
3
4
## Capabilities
5
6
### Is Core Function
7
8
Determines whether a given module name is a Node.js core module.
9
10
```javascript { .api }
11
/**
12
* Check if a module name is a Node.js core module
13
* @param {string} moduleName - Module name to check
14
* @returns {boolean} True if the module is a Node.js core module
15
*/
16
function isCore(moduleName);
17
```
18
19
**Usage Examples:**
20
21
```javascript
22
const resolve = require('resolve');
23
24
// Check common core modules
25
console.log(resolve.isCore('fs')); // true
26
console.log(resolve.isCore('path')); // true
27
console.log(resolve.isCore('http')); // true
28
console.log(resolve.isCore('crypto')); // true
29
30
// Check non-core modules
31
console.log(resolve.isCore('lodash')); // false
32
console.log(resolve.isCore('express')); // false
33
console.log(resolve.isCore('./local')); // false
34
35
// Check edge cases
36
console.log(resolve.isCore('node:fs')); // true (Node.js protocol prefix)
37
console.log(resolve.isCore('')); // false
38
```
39
40
### Core Module Map
41
42
Object containing a mapping of all core module names to boolean values indicating whether they are core modules.
43
44
```javascript { .api }
45
/**
46
* Object mapping core module names to boolean values
47
* Keys are module names, values are always true for core modules
48
* @type {object}
49
*/
50
const core;
51
```
52
53
**Usage Examples:**
54
55
```javascript
56
const resolve = require('resolve');
57
58
// Access the core modules object
59
console.log(resolve.core);
60
// {
61
// fs: true,
62
// path: true,
63
// http: true,
64
// https: true,
65
// crypto: true,
66
// ...
67
// }
68
69
// Check specific modules
70
if (resolve.core.fs) {
71
console.log('fs is a core module');
72
}
73
74
// Get all core module names
75
const coreModuleNames = Object.keys(resolve.core);
76
console.log('Core modules:', coreModuleNames);
77
78
// Filter user dependencies vs core modules
79
const userDependencies = ['lodash', 'fs', 'express', 'path']
80
.filter(name => !resolve.core[name]);
81
console.log('User dependencies:', userDependencies); // ['lodash', 'express']
82
```
83
84
## Integration with Resolution
85
86
Core module detection is automatically integrated into the main resolution functions and can be controlled via options.
87
88
### Include Core Modules Option
89
90
```javascript { .api }
91
interface CoreModuleOptions {
92
/** Include Node.js core modules in search (default: true) */
93
includeCoreModules?: boolean;
94
}
95
```
96
97
**Usage Examples:**
98
99
```javascript
100
const resolve = require('resolve');
101
102
// Default behavior - core modules are resolved
103
resolve('fs', { basedir: __dirname }, (err, res) => {
104
console.log(res); // 'fs' (core module name returned as-is)
105
});
106
107
// Exclude core modules from resolution
108
resolve('fs', {
109
basedir: __dirname,
110
includeCoreModules: false
111
}, (err, res) => {
112
// Will attempt to find 'fs' as a regular module in node_modules
113
// Likely to result in MODULE_NOT_FOUND error
114
});
115
116
// Sync version
117
try {
118
const res = resolve.sync('path', {
119
basedir: __dirname,
120
includeCoreModules: true
121
});
122
console.log(res); // 'path'
123
} catch (err) {
124
console.error(err);
125
}
126
```
127
128
## Core Module List
129
130
The resolve package includes detection for all Node.js core modules across different Node.js versions. Some examples include:
131
132
### Always Available Core Modules
133
- `fs` - File system operations
134
- `path` - Path utilities
135
- `http` - HTTP client/server
136
- `https` - HTTPS client/server
137
- `crypto` - Cryptographic functionality
138
- `events` - Event emitter
139
- `stream` - Streaming interfaces
140
- `buffer` - Binary data handling
141
- `util` - Utility functions
142
- `os` - Operating system utilities
143
144
### Version-Specific Core Modules
145
Some core modules were added in specific Node.js versions:
146
- `worker_threads` - Available in Node.js 10.5.0+
147
- `fs/promises` - Available in Node.js 10.0.0+
148
- `perf_hooks` - Available in Node.js 8.5.0+
149
150
### Node.js Protocol Prefix
151
152
Core modules can also be imported with the `node:` protocol prefix:
153
154
```javascript
155
const resolve = require('resolve');
156
157
// Both forms are recognized as core modules
158
console.log(resolve.isCore('fs')); // true
159
console.log(resolve.isCore('node:fs')); // true
160
161
resolve('node:fs', { basedir: __dirname }, (err, res) => {
162
console.log(res); // 'node:fs'
163
});
164
```
165
166
## Implementation Details
167
168
The core module detection uses the `is-core-module` package internally, which maintains an up-to-date list of Node.js core modules across different versions. This ensures accurate detection regardless of the Node.js version being used.
169
170
The core module map (`resolve.core`) is generated from the same data and provides O(1) lookup performance for core module checking in performance-critical scenarios.