mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
doc: add example of self-reference in scoped packages
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 <targos@protonmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com>
This commit is contained in:
committed by
Antoine du Hamel
parent
0e55a75ccd
commit
fe4d562dd9
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user