Language concepts
CommonJS require()
Node host function resolving and loading a module using CommonJS rules. Its interoperability with ES modules depends on the Node version and module graph. Not a browser-standard function. Multi-file example: save each listed file separately, then run the entry file with Node.js. See the packaged fixture for the original files.
Formal Syntax & Structure
javascript syntaxjavascript
require(specifier)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| specifier | string | Module identifier or relative path. |
Return Value
any
Exported value or host-defined interop result.
Working Example
Example Snippetjavascript
// File: calc.cjsmodule.exports = n => n * 4;
// File: main.cjsconst multiply = require("./calc.cjs");console.log(multiply(3));