Skip to content

Commit 2624c98

Browse files
pirannatargos
andcommitted
doc: add example of self-reference in scoped packages
PR-URL: #37630 Fixes: #37618 Refs: #37618 (comment) Co-authored-by: Michaël Zasso <[email protected]> Reviewed-By: Guy Bedford <[email protected]>
1 parent 00f2cee commit 2624c98

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: doc/api/packages.md

+26
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,32 @@ and in a CommonJS one. For example, this code will also work:
664664
const { something } = require('a-package/foo'); // Loads from ./foo.js.
665665
```
666666

667+
Finally, self-referencing also works with scoped packages. For example, this
668+
code will also work:
669+
670+
```json
671+
// package.json
672+
{
673+
"name": "@my/package",
674+
"exports": "./index.js"
675+
}
676+
```
677+
678+
```js
679+
// ./index.js
680+
module.exports = 42;
681+
```
682+
683+
```js
684+
// ./other.js
685+
console.log(require('@my/package'));
686+
```
687+
688+
```console
689+
$ node other.js
690+
42
691+
```
692+
667693
## Dual CommonJS/ES module packages
668694

669695
Prior to the introduction of support for ES modules in Node.js, it was a common

0 commit comments

Comments
 (0)