diff options
author | Jesús Leganés-Combarro 'piranna <piranna@gmail.com> | 2021-03-06 15:53:50 +0300 |
---|---|---|
committer | Antoine du Hamel <duhamelantoine1995@gmail.com> | 2021-08-17 12:19:22 +0300 |
commit | fe4d562dd95fb43961e43ec9b3799660b48e149e (patch) | |
tree | 730538b71d8bcbf5bef8b57ac833725d4c1c98db | |
parent | 0e55a75ccd3faa5a6d3c74d9fc701e6374ffb0de (diff) |
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 bd5fbe150b5..02884be8627 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 |