@@ -85,6 +85,41 @@ future-proof the package in case the default type of Node.js ever changes, and
8585it will also make things easier for build tools and loaders to determine how the
8686files in the package should be interpreted.
8787
88+ ### Modules loaders
89+
90+ Node.js has two system for resolving a specifier and load modules.
91+
92+ There is the CommonJS module loader:
93+
94+ * It is fully synchronous.
95+ * It is monkey patchable.
96+ * When resolving a specifier, if no exact match is found, it will try to add
97+ extensions (` .js ` , ` .json ` , and finally ` .node ` ).
98+ * It supports [ folders as modules] [ ] .
99+ * It treats ` .json ` as JSON text files.
100+ * ` .node ` files are interpreted as compiled addon modules loaded with
101+ ` process.dlopen() ` .
102+ * It treats files that do not have ` .json ` or ` .node ` extension as JavaScript
103+ text files.
104+ * It cannot be used to load ECMAScript modules. Attempting to do so will result
105+ in a [ ` ERR_REQUIRE_ESM ` ] [ ] error.
106+ * It can be accessed using ` require ` function.
107+
108+ There is the ECMAScript module loader:
109+
110+ * It is Asynchronous.
111+ * It is not monkey patchable, can be customized using [ loader hooks] [ ] .
112+ * No extension searching, the specifier must point to the exact URL of the file.
113+ * It does not support folders as modules.
114+ * Import assertion are needed to load JSON modules (behind
115+ ` --experimental-json-modules ` flag).
116+ * It only accepts ` .js ` , ` .mjs ` , and ` .cjs ` extensions for JavaScript text
117+ files.
118+ * It can be used to load (JavaScript) CommonJS modules. It passes the module
119+ content through the ` es-module-lexer ` to assess what are its exports, convert
120+ its URL to an absolute path and load it using the CommonJS module loader.
121+ * It can be accessed using ` import ` .
122+
88123### ` package.json ` and file extensions
89124
90125Within a package, the [ ` package.json ` ] [ ] [ ` "type" ` ] [ ] field defines how
@@ -1235,9 +1270,12 @@ This field defines [subpath imports][] for the current package.
12351270[ `--conditions` / `-C` flag ] : #resolving-user-conditions
12361271[ `--no-addons` flag ] : cli.md#--no-addons
12371272[ `ERR_PACKAGE_PATH_NOT_EXPORTED` ] : errors.md#err_package_path_not_exported
1273+ [ `ERR_REQUIRE_ESM` ] : errors.md#err_require_esm
12381274[ `esm` ] : https://github.com/standard-things/esm#readme
12391275[ `package.json` ] : #nodejs-packagejson-field-definitions
12401276[ entry points ] : #package-entry-points
1277+ [ folders as modules ] : modules.md#folders-as-modules
1278+ [ loader hooks ] : esm.md#loaders
12411279[ self-reference ] : #self-referencing-a-package-using-its-name
12421280[ subpath exports ] : #subpath-exports
12431281[ subpath imports ] : #subpath-imports
0 commit comments