From Nodejs docs https://nodejs.org/api/modules.html#all-together
require(X) from module at path Y
1. If X is a core module,
a. return the core module
b. STOP
2. If X begins with '/'
a. set Y to be the filesystem root
3. If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
c. THROW "not found"
4. If X begins with '#'
a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))
5. LOAD_PACKAGE_SELF(X, dirname(Y))
6. LOAD_NODE_MODULES(X, dirname(Y))
7. THROW "not found"
What does no.4 refer to?
What does it mean for a module to start with #
?
LOAD_PACKAGE_IMPORTS(X, DIR)
1. Find the closest package scope SCOPE to DIR.
2. If no scope was found, return.
3. If the SCOPE/package.json "imports" is null or undefined, return.
4. let MATCH = PACKAGE_IMPORTS_RESOLVE(X, pathToFileURL(SCOPE),
["node", "require"]) defined in the ESM resolver.
5. RESOLVE_ESM_MATCH(MATCH).
This is the LOAD_PACKAGE_IMPORTS
but it looks enigma to me.
The character "#" at the beginning of an import specifier refers to an imports field defined in package.json.
Imports fields are useful to alias file names or other dependencies that are only accessed inside a package.
Considering the example in the documentation:
"imports": {
"#dep": {
"node": "dep-node-native",
"default": "./dep-polyfill.js"
}
}
When a module in this package contains an import statement like
import dep from "#def";
This tells Node.js to import the package "dep-node-native"; and it instructs other build tools (for the browser) to use the local file "./dep-polyfill.js".