Konubinix' opinionated web of thoughts

Nodejs

Fleeting

Node.js is a JavaScript runtime built on the V8 JavaScript engine.

https://nodejs.org/api/documentation.html

hooks

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.

    https://nodejs.org/api/packages.html#determining-module-system

    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.

    https://nodejs.org/api/packages.html#determining-module-system

    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.

    https://nodejs.org/api/packages.html#determining-module-system

modules loaders

  • 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().
    • It treats all files that lack .json or .node extensions as JavaScript text files.
    • It cannot be used to load ECMAScript modules (although it is possible to load ECMASCript modules from CommonJS modules). When used to load a JavaScript text file that is not an ECMAScript module, it loads it as a CommonJS module.

    https://nodejs.org/api/packages.html#modules-loaders

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.
  • It accepts only .js, .mjs, and .cjs extensions for JavaScript text files.
  • It can be used to load JavaScript CommonJS modules. Such modules are passed through the cjs-module-lexer to try to identify named exports, which are available if they can be determined through static analysis. Imported CommonJS modules have their URLs converted to absolute paths and are then loaded via the CommonJS module loader.

https://nodejs.org/api/packages.html#modules-loaders

package.json “type” value of “module” tells Node.js to interpret .js files within that package as using ES module syntax

https://nodejs.org/api/packages.html#modules-loaders

If the nearest parent package.json lacks a “type” field, or contains “type”: “commonjs”, .js files are treated as CommonJS.

https://nodejs.org/api/packages.html#type

  • 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.

https://nodejs.org/api/packages.html#modules-loaders

package

package is a folder tree described by a package.json file

https://nodejs.org/api/packages.html#determining-module-system

Notes pointant ici