Node.js Reference ยท Module Systems
Node.js module.createRequire() method
Reference for module.createRequire() in Node.js: build a require function inside an ES module to load JSON, CommonJS packages and native addons.
createRequire() from node:module builds a require function bound to a file location. ES modules have no require, module or __filename; calling createRequire(import.meta.url) gives you a function that resolves and loads modules exactly as a CommonJS file next to your module would.
Reach for it when you need CommonJS behavior from an ES module: loading a .node addon, reading JSON synchronously, using require.resolve, or reaching into require.cache.
import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const data = require('./data.json');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
import { createRequire } from 'node:module'; createRequire(filename: string | URL): NodeJS.Require
| Name | Description |
|---|---|
filename |
An absolute path, a file: URL string or a URL object. Relative lookups start in the folder that contains this file. A path ending in a separator is treated as a folder. |
Returns: a require function with the usual extras: require.resolve(), require.resolve.paths(), require.cache and require.main.
Errors: ERR_INVALID_ARG_VALUE when filename is neither an absolute path nor a file: URL. The returned function throws MODULE_NOT_FOUND like any require.
Usage notes
- The file does not have to exist. Only the location matters, which is why a trailing separator or a made-up file name works.
- It shares the CommonJS cache. Modules loaded through it land in the same
require.cacheas for ordinary CommonJS files, so you get a single instance of a CommonJS dependency. - Name it
requireon purpose. Declaringconst require = createRequire(import.meta.url)at the top of a file reads naturally.requireis never a global in Node (the first example printsundefinedforglobalThis.require), so there is nothing to clash with. - Loading ES modules. Since
require(esm)landed in Node 22.12 and Node 24, the returned function can also load an ES module that has no top-levelawait. See require(esm). - Prefer
importwhere you can. JSON can be imported withimport data from './config.json' with { type: 'json' }. UsecreateRequirefor native addons, for synchronous lazy loading and for APIs that only exist onrequire. - Alternative in newer Node:
process.getBuiltinModule('node:fs')gives core modules without any import at all. See process.getBuiltinModule(). - Pitfall: passing
__filenameorimport.meta.filenameworks (they are absolute paths), but a relative string such as'./index.js'throws.