Skip to content

Commit 855dd86

Browse files
WIP
1 parent c61ae55 commit 855dd86

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

recipes/cjs-to-esm/README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11

22
# ESM Migration Codemod
33

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+
-->
512

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
714

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.
1224

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
1426

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

Comments
 (0)