Skip to content

Commit

Permalink
doc: add example of self-reference in scoped packages
Browse files Browse the repository at this point in the history
PR-URL: #37630
Fixes: #37618
Refs: #37618 (comment)
Co-authored-by: Michaël Zasso <[email protected]>
Reviewed-By: Guy Bedford <[email protected]>
  • Loading branch information
piranna and targos committed Aug 22, 2021
1 parent 7a25bf3 commit a387600
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/api/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a387600

Please sign in to comment.