From a387600d8fd6ca825dbea6a59913d6b78d5d1a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Sat, 6 Mar 2021 13:53:50 +0100 Subject: [PATCH] doc: add example of self-reference in scoped packages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Guy Bedford --- doc/api/packages.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/doc/api/packages.md b/doc/api/packages.md index bd5fbe150b544b..02884be862759c 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