|
| 1 | +# Modules: `module` API |
| 2 | + |
| 3 | +<!--introduced_in=v0.3.7--> |
| 4 | + |
| 5 | +## The `Module` object |
| 6 | + |
| 7 | +* {Object} |
| 8 | + |
| 9 | +Provides general utility methods when interacting with instances of |
| 10 | +`Module`, the `module` variable often seen in file modules. Accessed |
| 11 | +via `require('module')`. |
| 12 | + |
| 13 | +### `module.builtinModules` |
| 14 | +<!-- YAML |
| 15 | +added: |
| 16 | + - v9.3.0 |
| 17 | + - v8.10.0 |
| 18 | + - v6.13.0 |
| 19 | +--> |
| 20 | + |
| 21 | +* {string[]} |
| 22 | + |
| 23 | +A list of the names of all modules provided by Node.js. Can be used to verify |
| 24 | +if a module is maintained by a third party or not. |
| 25 | + |
| 26 | +`module` in this context isn't the same object that's provided |
| 27 | +by the [module wrapper][]. To access it, require the `Module` module: |
| 28 | + |
| 29 | +```js |
| 30 | +const builtin = require('module').builtinModules; |
| 31 | +``` |
| 32 | + |
| 33 | +### `module.createRequire(filename)` |
| 34 | +<!-- YAML |
| 35 | +added: v12.2.0 |
| 36 | +--> |
| 37 | + |
| 38 | +* `filename` {string|URL} Filename to be used to construct the require |
| 39 | + function. Must be a file URL object, file URL string, or absolute path |
| 40 | + string. |
| 41 | +* Returns: {require} Require function |
| 42 | + |
| 43 | +```js |
| 44 | +import { createRequire } from 'module'; |
| 45 | +const require = createRequire(import.meta.url); |
| 46 | + |
| 47 | +// sibling-module.js is a CommonJS module. |
| 48 | +const siblingModule = require('./sibling-module'); |
| 49 | +``` |
| 50 | +
|
| 51 | +### `module.createRequireFromPath(filename)` |
| 52 | +<!-- YAML |
| 53 | +added: v10.12.0 |
| 54 | +deprecated: v12.2.0 |
| 55 | +--> |
| 56 | +
|
| 57 | +> Stability: 0 - Deprecated: Please use [`createRequire()`][] instead. |
| 58 | +
|
| 59 | +* `filename` {string} Filename to be used to construct the relative require |
| 60 | + function. |
| 61 | +* Returns: {require} Require function |
| 62 | +
|
| 63 | +```js |
| 64 | +const { createRequireFromPath } = require('module'); |
| 65 | +const requireUtil = createRequireFromPath('../src/utils/'); |
| 66 | + |
| 67 | +// Require `../src/utils/some-tool` |
| 68 | +requireUtil('./some-tool'); |
| 69 | +``` |
| 70 | +
|
| 71 | +### `module.syncBuiltinESMExports()` |
| 72 | +<!-- YAML |
| 73 | +added: v12.12.0 |
| 74 | +--> |
| 75 | +
|
| 76 | +The `module.syncBuiltinESMExports()` method updates all the live bindings for |
| 77 | +builtin ES Modules to match the properties of the CommonJS exports. It does |
| 78 | +not add or remove exported names from the ES Modules. |
| 79 | +
|
| 80 | +```js |
| 81 | +const fs = require('fs'); |
| 82 | +const { syncBuiltinESMExports } = require('module'); |
| 83 | + |
| 84 | +fs.readFile = null; |
| 85 | + |
| 86 | +delete fs.readFileSync; |
| 87 | + |
| 88 | +fs.newAPI = function newAPI() { |
| 89 | + // ... |
| 90 | +}; |
| 91 | + |
| 92 | +syncBuiltinESMExports(); |
| 93 | + |
| 94 | +import('fs').then((esmFS) => { |
| 95 | + assert.strictEqual(esmFS.readFile, null); |
| 96 | + assert.strictEqual('readFileSync' in fs, true); |
| 97 | + assert.strictEqual(esmFS.newAPI, undefined); |
| 98 | +}); |
| 99 | +``` |
| 100 | +
|
| 101 | +## Source map v3 support |
| 102 | +<!-- YAML |
| 103 | +added: |
| 104 | + - v13.7.0 |
| 105 | + - v12.17.0 |
| 106 | +--> |
| 107 | +
|
| 108 | +> Stability: 1 - Experimental |
| 109 | +
|
| 110 | +Helpers for interacting with the source map cache. This cache is |
| 111 | +populated when source map parsing is enabled and |
| 112 | +[source map include directives][] are found in a modules' footer. |
| 113 | +
|
| 114 | +To enable source map parsing, Node.js must be run with the flag |
| 115 | +[`--enable-source-maps`][], or with code coverage enabled by setting |
| 116 | +[`NODE_V8_COVERAGE=dir`][]. |
| 117 | +
|
| 118 | +```js |
| 119 | +const { findSourceMap, SourceMap } = require('module'); |
| 120 | +``` |
| 121 | +
|
| 122 | +### `module.findSourceMap(path[, error])` |
| 123 | +<!-- YAML |
| 124 | +added: |
| 125 | + - v13.7.0 |
| 126 | + - v12.17.0 |
| 127 | +--> |
| 128 | +
|
| 129 | +* `path` {string} |
| 130 | +* `error` {Error} |
| 131 | +* Returns: {module.SourceMap} |
| 132 | +
|
| 133 | +`path` is the resolved path for the file for which a corresponding source map |
| 134 | +should be fetched. |
| 135 | +
|
| 136 | +The `error` instance should be passed as the second parameter to `findSourceMap` |
| 137 | +in exceptional flows, e.g., when an overridden |
| 138 | +[`Error.prepareStackTrace(error, trace)`][] is invoked. Modules are not added to |
| 139 | +the module cache until they are successfully loaded, in these cases source maps |
| 140 | +will be associated with the `error` instance along with the `path`. |
| 141 | +
|
| 142 | +### Class: `module.SourceMap` |
| 143 | +<!-- YAML |
| 144 | +added: |
| 145 | + - v13.7.0 |
| 146 | + - v12.17.0 |
| 147 | +--> |
| 148 | +
|
| 149 | +#### `new SourceMap(payload)` |
| 150 | +
|
| 151 | +* `payload` {Object} |
| 152 | +
|
| 153 | +Creates a new `sourceMap` instance. |
| 154 | +
|
| 155 | +`payload` is an object with keys matching the [Source map v3 format][]: |
| 156 | +
|
| 157 | +* `file`: {string} |
| 158 | +* `version`: {number} |
| 159 | +* `sources`: {string[]} |
| 160 | +* `sourcesContent`: {string[]} |
| 161 | +* `names`: {string[]} |
| 162 | +* `mappings`: {string} |
| 163 | +* `sourceRoot`: {string} |
| 164 | +
|
| 165 | +#### `sourceMap.payload` |
| 166 | +
|
| 167 | +* Returns: {Object} |
| 168 | +
|
| 169 | +Getter for the payload used to construct the [`SourceMap`][] instance. |
| 170 | +
|
| 171 | +#### `sourceMap.findEntry(lineNumber, columnNumber)` |
| 172 | +
|
| 173 | +* `lineNumber` {number} |
| 174 | +* `columnNumber` {number} |
| 175 | +* Returns: {Object} |
| 176 | +
|
| 177 | +Given a line number and column number in the generated source file, returns |
| 178 | +an object representing the position in the original file. The object returned |
| 179 | +consists of the following keys: |
| 180 | +
|
| 181 | +* generatedLine: {number} |
| 182 | +* generatedColumn: {number} |
| 183 | +* originalSource: {string} |
| 184 | +* originalLine: {number} |
| 185 | +* originalColumn: {number} |
| 186 | +
|
| 187 | +[`createRequire()`]: #module_module_createrequire_filename |
| 188 | +[module wrapper]: modules_cjs.html#modules_cjs_the_module_wrapper |
| 189 | +[source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx |
| 190 | +[`--enable-source-maps`]: cli.html#cli_enable_source_maps |
| 191 | +[`NODE_V8_COVERAGE=dir`]: cli.html#cli_node_v8_coverage_dir |
| 192 | +[`Error.prepareStackTrace(error, trace)`]: https://v8.dev/docs/stack-trace-api#customizing-stack-traces |
| 193 | +[`SourceMap`]: #module_class_module_sourcemap |
| 194 | +[Source map v3 format]: https://sourcemaps.info/spec.html#h.mofvlxcwqzej |
0 commit comments