Skip to content

Commit 5bb6955

Browse files
authored
add chapter 5: cjs to esm migration (#16)
* add chapter 5: cjs to esm migration * fixup! add chapter 5: cjs to esm migration
1 parent 97b3d69 commit 5bb6955

108 files changed

Lines changed: 1494 additions & 0 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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
description: Guide on migrating CommonJS packages to ESM.
3+
---
4+
5+
# Migrating CommonJS to ESM
6+
7+
Migrating a package from CommonJS to ESM often requires code changes due to semantic differences. Let's explore the common patterns in this chapter.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
description: Guide on migrating context-local variables in CommonJS to ESM.
3+
---
4+
5+
# Migrating context-local variables in CommonJS
6+
7+
Examples in this chapter can be found [here](https://github.com/nodejs/package-examples/blob/main/guide/05-cjs-esm-migration/migrating-context-local-variables/).
8+
9+
In CommonJS modules, there are several context-local variables that provide information about the current module and its execution context. When migrating to ESM, these variables need to be replaced with their ESM equivalents.
10+
11+
In Node.js, most of the replacements are provided through the [`import.meta`](https://nodejs.org/api/esm.html#importmeta) object.
12+
13+
| CommonJS | ESM Equivalent | Minimum Node.js Version |
14+
|----------|----------------|------------------------|
15+
| `__filename` | `import.meta.filename` | v20.11.0 / v21.2.0 |
16+
| `__dirname` | `import.meta.dirname` | v20.11.0 / v21.2.0 |
17+
| `require.main` | `import.meta.main` | v22.18.0 / v24.2.0 |
18+
| `require.resolve` | `import.meta.resolve` | v12.16.2+ |
19+
20+
For older versions of Node.js where `import.meta.filename` and `import.meta.dirname` are not available, `import.meta.url` can be used to derive the filename and dirname using `fileURLToPath()` from `node:url`.
21+
22+
```js
23+
import { fileURLToPath } from 'node:url';
24+
import { dirname } from 'node:path';
25+
const __filename = fileURLToPath(import.meta.url);
26+
const __dirname = dirname(__filename);
27+
```
28+
29+
In the case of `module` and `exports` variables, writes to them typically are done to set up exports for the module, in which case these usually can be replaced with `export` statements. See [Migrating exports](../migrating-exports/README.md) for details. ESM does not support reflection on module exports, so reads from these variables have no direct replacements in ESM.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getModuleInfo } from 'my-module/index-fallback.js';
2+
const info = getModuleInfo();
3+
console.log('Filename:', info.filename);
4+
console.log('Dirname:', info.dirname);
5+
console.log('URL:', info.url);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { getModuleInfo } from 'my-module';
2+
const info = getModuleInfo();
3+
console.log('Filename:', info.filename);
4+
console.log('Dirname:', info.dirname);
5+
console.log('Is main from module: ' + info.isMain);
6+
console.log('Is main from main: ' + import.meta.main);

guide/05-cjs-esm-migration/migrating-context-local-variables/after/node_modules/my-module/index-fallback.js

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

guide/05-cjs-esm-migration/migrating-context-local-variables/after/node_modules/my-module/index.js

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

guide/05-cjs-esm-migration/migrating-context-local-variables/after/node_modules/my-module/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { getModuleInfo } = require('my-module');
2+
const info = getModuleInfo();
3+
console.log('Filename:', info.filename);
4+
console.log('Dirname:', info.dirname);
5+
console.log('Is main from module: ' + info.isMain);
6+
console.log('Is main from main: ' + (require.main === module));

guide/05-cjs-esm-migration/migrating-context-local-variables/before/node_modules/my-module/index.js

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

guide/05-cjs-esm-migration/migrating-context-local-variables/before/node_modules/my-module/package.json

Lines changed: 5 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)