diff options
author | Jesús Leganés-Combarro 'piranna <piranna@gmail.com> | 2021-03-06 13:53:50 +0100 |
---|---|---|
committer | Michaël Zasso <targos@protonmail.com> | 2021-08-22 08:48:26 +0200 |
commit | a387600d8fd6ca825dbea6a59913d6b78d5d1a30 (patch) | |
tree | eb9ca0c2f040f2f5a719284b8f3c879acc0a571f | |
parent | 7a25bf3a6da061cf94b7c9800fa345dba71006ef (diff) | |
download | node-new-a387600d8fd6ca825dbea6a59913d6b78d5d1a30.tar.gz |
doc: add example of self-reference in scoped packages
PR-URL: https://github.com/nodejs/node/pull/37630
Fixes: https://github.com/nodejs/node/issues/37618
Refs: https://github.com/nodejs/node/issues/37618#issuecomment-791888177
Co-authored-by: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
-rw-r--r-- | doc/api/packages.md | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/doc/api/packages.md b/doc/api/packages.md index bd5fbe150b..02884be862 100644 --- a/doc/api/packages.md +++ b/doc/api/packages.md @@ -709,6 +709,32 @@ and in a CommonJS one. For example, this code will also work: const { something } = require('a-package/foo'); // Loads from ./foo.js. ``` +Finally, self-referencing also works with scoped packages. For example, this +code will also work: + +```json +// package.json +{ + "name": "@my/package", + "exports": "./index.js" +} +``` + +```js +// ./index.js +module.exports = 42; +``` + +```js +// ./other.js +console.log(require('@my/package')); +``` + +```console +$ node other.js +42 +``` + ## Dual CommonJS/ES module packages Prior to the introduction of support for ES modules in Node.js, it was a common |