summaryrefslogtreecommitdiff
path: root/doc/api/modules.md
diff options
context:
space:
mode:
authorJan Krems <jan.krems@gmail.com>2019-08-19 20:59:25 -0700
committerJan Krems <jan.krems@gmail.com>2019-10-24 15:14:38 -0700
commit71bcd05232b4fc21db20e5acf019f97780050568 (patch)
treeff58c1a5c27e3e28755395e1b07b0394583d96e1 /doc/api/modules.md
parentd53dd8b0a00d3e00e97f46ae4ae67afa31c10526 (diff)
downloadandroid-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.tar.gz
android-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.tar.bz2
android-node-v8-71bcd05232b4fc21db20e5acf019f97780050568.zip
module: resolve self-references
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: https://github.com/nodejs/node/pull/29327 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'doc/api/modules.md')
-rw-r--r--doc/api/modules.md12
1 files changed, 10 insertions, 2 deletions
diff --git a/doc/api/modules.md b/doc/api/modules.md
index cb14c8face..81f9d2d853 100644
--- a/doc/api/modules.md
+++ b/doc/api/modules.md
@@ -159,8 +159,9 @@ require(X) from module at path Y
3. If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
-4. LOAD_NODE_MODULES(X, dirname(Y))
-5. THROW "not found"
+5. LOAD_NODE_MODULES(X, dirname(Y))
+4. LOAD_SELF_REFERENCE(X, dirname(Y))
+6. THROW "not found"
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
@@ -200,6 +201,13 @@ NODE_MODULES_PATHS(START)
c. DIRS = DIRS + DIR
d. let I = I - 1
5. return DIRS
+
+LOAD_SELF_REFERENCE(X, START)
+1. Find the closest package scope to START.
+2. If no scope was found, throw "not found".
+3. If the name in `package.json` isn't a prefix of X, throw "not found".
+4. Otherwise, resolve the remainder of X relative to this package as if it
+ was loaded via `LOAD_NODE_MODULES` with a name in `package.json`.
```
Node.js allows packages loaded via