Search This Blog

2024/03/19

Node.js:Types of module in node.js

Node.js has two module systems: CommonJS modules and ECMAScript modules.


By default, Node.js will treat the following as CommonJS modules:

Files with a .cjs extension;

Files with a .js extension when the nearest parent package.json
file contains a top-level field "type" with a value of "commonjs".

Files with a .js extension or without an extension, when the
nearest parent package.json file doesn't contain a top-level
field "type" or there is no package.json in any parent folder;
unless the file contains syntax that errors unless it is evaluated
as an ES module. Package authors should include the "type" field,
even in packages where all sources are CommonJS. Being explicit
about the type of the package will make things easier for build
tools and loaders to determine how the files in the package should be interpreted.

Files with an extension that is not .mjs, .cjs, .json, .node, or .js
(when the nearest parent package.json file contains a top-level field
"type" with a value of "module", those files will be recognized as
CommonJS modules only if they are being included via require(),
not when used as the command-line entry point of the program).

Calling require() always use the CommonJS module loader.
Calling import() always use the ECMAScript module loader.

Due to the synchronous nature of require(), it is not possible to use
it to load ECMAScript module files. Attempting to do so will throw a
ERR_REQUIRE_ESM error. Use import() instead.

The .mjs extension is reserved for ECMAScript Modules which cannot be loaded via
require().

No comments:

Post a Comment