|
1 | 1 |
|
2 | 2 | # ESM Migration Codemod |
3 | 3 |
|
4 | | -## Context-local Variable Migration |
| 4 | +<!-- |
| 5 | + TODO orrect reamde: |
| 6 | + - Remove "How it's will/should works" that's development notes |
| 7 | + - Improve "Limitations" section |
| 8 | + - Add Why/when to use this codemod |
| 9 | + - Add example usage |
| 10 | + - Add description of options |
| 11 | +--> |
5 | 12 |
|
6 | | -This codemod includes a step to help migrate context-local variables and Node.js built-in globals from CommonJS to ESM. It detects usages of: |
| 13 | +## How it's will/should works |
7 | 14 |
|
8 | | -- `__filename` → `import.meta.filename` (Node.js v20.11.0 / v21.2.0) |
9 | | -- `__dirname` → `import.meta.dirname` (Node.js v20.11.0 / v21.2.0) |
10 | | -- `require.main` → `import.meta.main` (Node.js v22.18.0 / v24.2.0) |
11 | | -- `require.resolve` → `import.meta.resolve` (available) |
| 15 | +1. Change file extension from `.cjs` to `.js` & `.mjs` to `.js`. And IDK how keep track of this change. |
| 16 | +2. Change importing files to use ESM syntax. With updating the specifier to reflect file extension changes. |
| 17 | +3. Change exporting files to use ESM syntax. |
| 18 | +4. Update context-local variables. If possible to track this change in goal of having the lowest nodejs version in the `engines` field of `package.json`. |
| 19 | +5. Update `package.json`: |
| 20 | + - Add/update `"type": "module"` field. |
| 21 | + - Update file extensions in `"main"`, `"module"`, `"exports"`, and other relevant fields. |
| 22 | + - _not sure_ Remove `"exports"` field if it only contains CJS-specific entries. |
| 23 | + - update engines field to reflect the minimum Node.js version that supports all ESM features used in the codebase. |
12 | 24 |
|
13 | | -If these or other context-local patterns are found, the codemod will emit a warning and suggest reviewing the [migration guide](https://github.com/nodejs/package-examples/blob/main/guide/05-cjs-esm-migration/migrating-context-local-variables/README.md). |
| 25 | +## Limitations |
14 | 26 |
|
15 | | -**Note:** Some features require specific Node.js versions as indicated above. |
| 27 | +- Typescript: its will be more complex because we need to update the whole building process. |
| 28 | + |
| 29 | +## REFS |
| 30 | + |
| 31 | +- https://nodejs.github.io/package-examples/05-cjs-esm-migration/ |
| 32 | +- https://nodejs.github.io/package-examples/05-cjs-esm-migration/migrating-imports/ |
| 33 | +- https://nodejs.github.io/package-examples/05-cjs-esm-migration/migrating-exports/ |
| 34 | +- https://nodejs.github.io/package-examples/05-cjs-esm-migration/migrating-context-local-variables/ |
| 35 | +- https://nodejs.github.io/package-examples/05-cjs-esm-migration/migrating-package-json/ |
0 commit comments