Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

<!--introduced_in=v12.20.0-->

> Stability: 2 - Stable

<!-- source_link=lib/module.js -->

The `node:module` module provides utilities for interacting with the Node.js
module system.

It can be accessed using:

```mjs
import module from 'node:module';
```

## Class: `Module`

<!-- YAML
added: v0.3.7
added: v0.3.5
-->

## The `Module` object
`module` objects in [CommonJS][] modules are instances of this class.

* Type: {Object}
See the CommonJS documentation for [the `module` object][].

Provides general utility methods when interacting with instances of
`Module`, the [`module`][] variable often seen in [CommonJS][] modules. Accessed
via `import 'node:module'` or `require('node:module')`.

### `module.builtinModules`
## `module.builtinModules`

<!-- YAML
added:
Expand All @@ -29,25 +40,18 @@ changes:

* Type: {string\[]}

A list of the names of all modules provided by Node.js. Can be used to verify
if a module is maintained by a third party or not.

`module` in this context isn't the same object that's provided
by the [module wrapper][]. To access it, require the `Module` module:
A list of the names of all modules provided by Node.js.

```mjs
// module.mjs
// In an ECMAScript module
import { builtinModules as builtin } from 'node:module';
import { builtinModules } from 'node:module';
console.log(builtinModules.filter((name) => name.startsWith('path')));
// Prints: [ 'path', 'path/posix', 'path/win32' ]
```

```cjs
// module.cjs
// In a CommonJS module
const builtin = require('node:module').builtinModules;
```
To test whether a module name corresponds to a builtin module, use
[`module.isBuiltin()`][].

### `module.createRequire(filename)`
## `module.createRequire(filename)`

<!-- YAML
added: v12.2.0
Expand All @@ -66,7 +70,7 @@ const require = createRequire(import.meta.url);
const siblingModule = require('./sibling-module');
```

### `module.findPackageJSON(specifier[, base])`
## `module.findPackageJSON(specifier[, base])`

<!-- YAML
added:
Expand Down Expand Up @@ -154,7 +158,7 @@ findPackageJSON('@foo/qux', __filename);
// '/path/to/project/packages/qux/package.json'
```

### `module.isBuiltin(moduleName)`
## `module.isBuiltin(moduleName)`

<!-- YAML
added:
Expand All @@ -172,7 +176,7 @@ isBuiltin('fs'); // true
isBuiltin('wss'); // false
```

### `module.register(specifier[, parentURL][, options])`
## `module.register(specifier[, parentURL][, options])`

<!-- YAML
added:
Expand Down Expand Up @@ -222,7 +226,7 @@ resolution and loading behavior. See [Customization hooks][].

This feature requires `--allow-worker` if used with the [Permission Model][].

### `module.registerHooks(options)`
## `module.registerHooks(options)`

<!-- YAML
added:
Expand Down Expand Up @@ -251,7 +255,7 @@ Register [hooks][] that customize Node.js module resolution and loading behavior
See [Customization hooks][]. The returned object can be used to
[deregister the hooks][deregistration of synchronous customization hooks].

### `module.stripTypeScriptTypes(code[, options])`
## `module.stripTypeScriptTypes(code[, options])`

<!-- YAML
added:
Expand Down Expand Up @@ -316,7 +320,7 @@ console.log(strippedCode);
// Prints: const a = 1\n\n//# sourceURL=source.ts;
```

### `module.syncBuiltinESMExports()`
## `module.syncBuiltinESMExports()`

<!-- YAML
added: v12.12.0
Expand Down Expand Up @@ -2094,9 +2098,9 @@ returned object contains the following keys:
[`module.enableCompileCache()`]: #moduleenablecompilecacheoptions
[`module.flushCompileCache()`]: #moduleflushcompilecache
[`module.getCompileCacheDir()`]: #modulegetcompilecachedir
[`module.isBuiltin()`]: #moduleisbuiltinmodulename
[`module.registerHooks()`]: #moduleregisterhooksoptions
[`module.setSourceMapsSupport()`]: #modulesetsourcemapssupportenabled-options
[`module`]: #the-module-object
[`os.tmpdir()`]: os.md#ostmpdir
[`register`]: #moduleregisterspecifier-parenturl-options
[`util.TextDecoder`]: util.md#class-utiltextdecoder
Expand All @@ -2109,11 +2113,11 @@ returned object contains the following keys:
[hooks]: #customization-hooks
[load hook]: #synchronous-loadurl-context-nextload
[module compile cache]: #module-compile-cache
[module wrapper]: modules.md#the-module-wrapper
[realm]: https://tc39.es/ecma262/#realm
[resolve hook]: #synchronous-resolvespecifier-context-nextresolve
[source map include directives]: https://tc39.es/ecma426/#sec-linking-generated-code
[synchronous hook functions]: #hook-functions-accepted-by-moduleregisterhooks
[the `module` object]: modules.md#the-module-object
[the documentation of `Worker`]: worker_threads.md#new-workerfilename-options
[transferable objects]: worker_threads.md#portpostmessagevalue-transferlist
[type-stripping]: typescript.md#type-stripping
Loading