Skip to content

Commit a18d1ba

Browse files
committed
[doc] Example of self-reference in scoped packages
Original code by Michaël Zasso 'targos' from nodejs#37618 (comment) Closes nodejs#37618
1 parent eb3c8cf commit a18d1ba

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
@@ -683,6 +683,32 @@ and in a CommonJS one. For example, this code will also work:
683683
const { something } = require('a-package/foo'); // Loads from ./foo.js.
684684
```
685685

686+
Finally, self-referencing also works with scoped packages. For example, this
687+
code will also work:
688+
689+
```json
690+
// package.json
691+
{
692+
"name": "@my/package",
693+
"exports": "./index.js"
694+
}
695+
```
696+
697+
```js
698+
// ./index.js
699+
module.exports = 42
700+
```
701+
702+
```js
703+
// ./other.js
704+
console.log(require('@my/package'))
705+
```
706+
707+
```sh
708+
node other.js
709+
# 42
710+
```
711+
686712
## Dual CommonJS/ES module packages
687713

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

0 commit comments

Comments
 (0)