Modules
module.createRequire()
Creates CommonJS require anchored to a file location for controlled ESM interoperability. Never resolve arbitrary untrusted paths. Tested on Node.js 22.16.0. Example output (one line per entry): ["function"].
Formal Syntax & Structure
nodejs syntaxnodejs
module.createRequire(filename)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| filename | string | URL | Absolute filename or file URL. |
Return Value
Function
Anchored require function.
Working Example
Example Snippetnodejs
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);console.log(typeof require('node:path').join);