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:
Jesús Leganés-Combarro 'piranna
2021-03-06 13:53:50 +01:00
committed by Antoine du Hamel
parent 0e55a75ccd
commit fe4d562dd9

View File

@@ -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