Skip to content

Commit fe4d562

Browse files
pirannatargos
authored 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 0e55a75 commit fe4d562

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/api/packages.md

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

712+
Finally, self-referencing also works with scoped packages. For example, this
713+
code will also work:
714+
715+
```json
716+
// package.json
717+
{
718+
"name": "@my/package",
719+
"exports": "./index.js"
720+
}
721+
```
722+
723+
```js
724+
// ./index.js
725+
module.exports = 42;
726+
```
727+
728+
```js
729+
// ./other.js
730+
console.log(require('@my/package'));
731+
```
732+
733+
```console
734+
$ node other.js
735+
42
736+
```
737+
712738
## Dual CommonJS/ES module packages
713739

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

0 commit comments

Comments
 (0)