Skip to content

Commit 01d632c

Browse files
authored
Use gitbook-plugin-include-codeblock (#21)
1 parent c398755 commit 01d632c

52 files changed

Lines changed: 1058 additions & 695 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

guide/02-single-file-package/classic-commonjs/README.md

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,15 @@ This is the most traditional way of creating a package in Node.js, using the Com
88

99
In this example, the `my-logger` package consists of a single file, logger.js, that implements and exports the `Logger` class.
1010

11-
```js
12-
// logger.js
13-
class Logger {
14-
// ... Logger implementation
15-
};
16-
exports.Logger = Logger;
17-
```
11+
`logger.js`:
12+
13+
[import:'logger_start,logger_end'](node_modules/my-logger/logger.js)
1814

1915
The `package.json` file for this package looks like this:
2016

21-
```json
22-
{
23-
"name": "my-logger",
24-
"version": "1.0.0",
25-
"type": "commonjs",
26-
"exports": {
27-
".": "./logger.js",
28-
"./package.json": "./package.json"
29-
}
30-
}
31-
```
17+
`package.json`:
18+
19+
[import](node_modules/my-logger/package.json)
3220

3321
So the package structure is as follows:
3422

@@ -47,21 +35,15 @@ In addition, we have included an export for `./package.json` in the `"exports"`
4735

4836
When users load this package, they can do so like this:
4937

50-
```js
51-
// app.cjs
52-
const { Logger } = require('my-logger');
53-
const logger = new Logger('debug');
54-
logger.error('This is an error message');
55-
```
38+
`app.cjs`:
39+
40+
[import:'doc'](app.cjs)
5641

5742
Or, a ESM consumer can load this package like this:
5843

59-
```js
60-
// app.mjs
61-
import { Logger } from 'my-logger';
62-
const logger = new Logger('debug');
63-
logger.error('This is an error message');
64-
```
44+
`app.mjs`:
45+
46+
[import](app.mjs)
6547

6648
Usually, a published package is placed in a `node_modules` directory of a project. When you use `require('my-logger')`, Node.js will start looking for a directory named `my-logger` in the nearest `node_modules` directory up until it reaches the root of the filesystem. You can refer to the [Node.js documentation](https://nodejs.org/api/modules.html#all-together) for more details about the module resolution algorithm.
6749

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
//! [doc]
34
const { Logger } = require('my-logger');
45
const logger = new Logger('debug');
56
logger.error('This is an error message');
7+
//! [doc]

guide/02-single-file-package/classic-commonjs/node_modules/my-logger/logger.js

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guide/02-single-file-package/simple-esm/README.md

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,15 @@ This is the more modern way of creating a package in Node.js, using the ESM syst
88

99
In this example, we have a package named `my-logger` that exports a `Logger` class. The package consists of a single file, `logger.js`, which contains the implementation of the `Logger` class.
1010

11-
```js
12-
// logger.js
13-
export class Logger {
14-
// ... Logger implementation
15-
};
16-
```
11+
`logger.js`:
12+
13+
[import:'logger_start,logger_end'](node_modules/my-logger/logger.js)
1714

1815
The `package.json` file for this package looks like this:
1916

20-
```json
21-
{
22-
"name": "my-logger",
23-
"version": "1.0.0",
24-
"type": "module",
25-
"exports": {
26-
".": "./logger.js",
27-
"./package.json": "./package.json"
28-
}
29-
}
30-
```
17+
`package.json`:
18+
19+
[import](node_modules/my-logger/package.json)
3120

3221
So the package structure is as follows:
3322

@@ -46,21 +35,15 @@ In addition, we have included an export for `./package.json` in the `"exports"`
4635

4736
When users load this package, they can do so like this:
4837

49-
```js
50-
// app.mjs
51-
import { Logger } from 'my-logger';
52-
const logger = new Logger('debug');
53-
logger.error('This is an error message');
54-
```
38+
`app.mjs`:
39+
40+
[import](app.mjs)
5541

5642
Or, from Node.js 20 and above, CommonJS consumers can also load a ESM package like this:
5743

58-
```js
59-
// app.cjs
60-
const { Logger } = require('my-logger');
61-
const logger = new Logger('debug');
62-
logger.error('This is an error message');
63-
```
44+
`app.cjs`:
45+
46+
[import:'doc'](app.cjs)
6447

6548
Usually, a published package is placed in a `node_modules` directory of a project. When you use `import 'my-logger'`, Node.js will start looking for a directory named `my-logger` in the nearest `node_modules` directory up until it reaches the root of the filesystem. The module resolution algorithm for ESM is slightly different from the one used for loading CommonJS modules. You can refer to the [Node.js documentation](https://nodejs.org/api/esm.html#resolution-algorithm-specification) for more details.
6649

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
//! [doc]
34
const { Logger } = require('my-logger');
45
const logger = new Logger('debug');
56
logger.error('This is an error message');
7+
//! [doc]

guide/02-single-file-package/simple-esm/node_modules/my-logger/logger.js

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

guide/03-multi-file-package/README.md

Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,23 @@ my-logger/
1717
└── package.json
1818
```
1919

20-
```js
21-
// lib/utils.js
22-
export const LEVELS = { /* ... log levels ... */ };
23-
```
20+
`lib/utils.js`:
2421

25-
```js
26-
// src/logger.js
27-
import { LEVELS } from '../lib/utils.js';
28-
export class Logger {
29-
// ... Logger implementation
30-
};
31-
```
22+
[import:'abbrev,abbrev_end'](example/node_modules/my-logger/lib/utils.js)
3223

33-
```js
34-
// index.js
35-
export { Logger } from './src/logger.js';
36-
```
24+
`src/logger.js`:
25+
26+
[import:'logger_start,logger_end'](example/node_modules/my-logger/src/logger.js)
27+
28+
`index.js`:
29+
30+
[import](example/node_modules/my-logger/index.js)
3731

3832
The `package.json` file for this package would typically look like this:
3933

40-
```json
41-
{
42-
"name": "my-logger",
43-
"version": "1.0.0",
44-
"type": "module",
45-
"scripts": {
46-
"test": "node --test"
47-
},
48-
"exports": {
49-
".": "./index.js",
50-
"./package.json": "./package.json"
51-
},
52-
"files": [
53-
"lib/",
54-
"src/",
55-
"index.js"
56-
]
57-
}
58-
```
34+
`package.json`:
35+
36+
[import](example/node_modules/my-logger/package.json)
5937

6038
When this package is published, because `test` is not listed in `files`, this directory will be excluded in the published package. Here we use `npm pack` to verify it (though this convention is generally respected by most package managers):
6139

@@ -74,26 +52,14 @@ package/package.json
7452

7553
In addition, when users load this package, they can only access `index.js` and `package.json`, but not the internal files. This allows maintainers to change the internal structure of the package without breaking users who may come to assume the internal files are part of the public API.
7654

77-
```js
78-
// app.mjs
79-
import { Logger } from 'my-logger';
80-
const logger = new Logger('debug');
81-
logger.error('This is an error message');
55+
`app.mjs`:
8256

83-
// This will throw ERR_PACKAGE_PATH_NOT_EXPORTED
84-
await import('my-logger/lib/utils.js');
85-
```
57+
[import](example/app.mjs)
8658

8759
This works similarly for CommonJS consumers (since the package is ESM, they will need to use Node.js 20 or above to load it from `require()`):
8860

89-
```js
90-
// app.cjs
91-
const { Logger } = require('my-logger');
92-
const logger = new Logger('debug');
93-
logger.error('This is an error message');
61+
`app.cjs`:
9462

95-
// This will throw ERR_PACKAGE_PATH_NOT_EXPORTED
96-
require('my-logger/lib/utils.js');
97-
```
63+
[import:'doc'](example/app.cjs)
9864

9965
You can find an example of this package on [GitHub](https://github.com/nodejs/package-examples/tree/main/guide/03-multi-file-package/example).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
'use strict';
22

3+
//! [doc]
34
const { Logger } = require('my-logger');
45
const logger = new Logger('debug');
56
logger.error('This is an error message');
7+
8+
try {
9+
// This will throw ERR_PACKAGE_PATH_NOT_EXPORTED
10+
require('my-logger/lib/utils.js');
11+
} catch (e) {
12+
console.error(e.code, e.message);
13+
}
14+
//! [doc]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
import { Logger } from 'my-logger';
22
const logger = new Logger('debug');
33
logger.error('This is an error message');
4+
5+
try {
6+
// This will throw ERR_PACKAGE_PATH_NOT_EXPORTED
7+
await import('my-logger/lib/utils.js');
8+
} catch (e) {
9+
console.error(e.code, e.message);
10+
}

guide/03-multi-file-package/example/node_modules/my-logger/lib/utils.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)