Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import { createRequire } from 'node:module';

const require = createRequire(import.meta.url);
const data = require('./data.json');

Try it

Node.js require inside an ES module

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.

Node.js Choosing the base location

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

NODE
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.cache as for ordinary CommonJS files, so you get a single instance of a CommonJS dependency.
  • Name it require on purpose. Declaring const require = createRequire(import.meta.url) at the top of a file reads naturally. require is never a global in Node (the first example prints undefined for globalThis.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-level await. See require(esm).
  • Prefer import where you can. JSON can be imported with import data from './config.json' with { type: 'json' }. Use createRequire for native addons, for synchronous lazy loading and for APIs that only exist on require.
  • Alternative in newer Node: process.getBuiltinModule('node:fs') gives core modules without any import at all. See process.getBuiltinModule().
  • Pitfall: passing __filename or import.meta.filename works (they are absolute paths), but a relative string such as './index.js' throws.