Référence externe : https://nodejs.org/api/esm.html#loaders
When hooks are used they apply to each subsequent loader, the entry point, and all import calls. They won’t apply to require calls; those still follow CommonJS rules
determining module system
Référence externe : https://nodejs.org/api/packages.html#determining-module-system
Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements or import() expressions:
- Files with an .mjs extension.
- Files with a .js extension when the nearest parent package.json file contains a top-level “type” field with a value of “module”.
- Strings passed in as an argument to –eval, or piped to node via STDIN, with the flag –input-type=module.
Node.js will treat as CommonJS all other forms of input, such as .js files where the nearest parent package.json file contains no top-level “type” field, or string input without the flag –input-type.
Node.js will treat the following as CommonJS when passed to node as the initial input, or when referenced by import statements, import() expressions, or require() expressions:
- 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”.
- Strings passed in as an argument to –eval or –print, or piped to node via STDIN, with the flag –input-type=commonjs.
Référence externe : https://nodejs.org/api/packages.html#modules-loaders CommonJS module loader:
- It is fully synchronous.
- It is responsible for handling require() calls.
- It is monkey patchable.
- It supports folders as modules.
- When resolving a specifier, if no exact match is found, it will try to add extensions (.js, .json, and finally .node) and then attempt to resolve folders as modules.
- It treats .json as JSON text files.
- .node files are interpreted as compiled addon modules loaded with process.dlopen().
ECMAScript module loader:
- It is asynchronous.
- It is responsible for handling import statements and import() expressions.
- It is not monkey patchable, can be customized using loader hooks.
- It does not support folders as modules, directory indexes (e.g. ‘./startup/index.js’) must be fully specified.
- It does no extension searching. A file extension must be provided when the specifier is a relative or absolute file URL.
- It can load JSON modules, but an import assertion is required.
package.json “type” value of “module” tells Node.js to interpret .js files within that package as using ES module syntax
- Files ending with .mjs are always loaded as ES modules regardless of the nearest parent package.json.
- Files ending with .cjs are always loaded as CommonJS regardless of the nearest parent package.json.
package is a folder tree described by a package.json file
Notes pointant ici
- does nodejs wait for all promises before exiting? (blog)
- how to connect to a websocket with a self signed certificate in nodejs
- Node.js Event Loop, Timers, and process.nextTick()
- trying to make programming easier