Skip to content

Commit e863215

Browse files
committed
docs: add instructions to support moduleResolution: node
1 parent f1db236 commit e863215

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

docs/pages/esm.md

+52-19
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,7 @@ To configure a dual package setup, you can follow these steps:
8282
}
8383
```
8484

85-
2. Optionally change the `main` field in your `package.json` to point to the CommonJS build:
86-
87-
```diff
88-
- "main": "./lib/module/index.js",
89-
+ "main": "./lib/commonjs/index.js",
90-
```
91-
92-
This is needed if you want to support tools that don't support the `exports` field and need to use the CommonJS build.
93-
94-
3. Optionally add a `module` field in your `package.json` to point to the ESM build:
95-
96-
```diff
97-
"main": "./lib/commonjs/index.js",
98-
+ "module": "./lib/module/index.js",
99-
```
100-
101-
The `module` field is a non-standard field that some tools use to determine the ESM entry point.
102-
103-
4. Change the `exports` field in your `package.json` to include 2 conditions:
85+
2. Change the `exports` field in your `package.json` to include 2 conditions:
10486

10587
```diff
10688
"exports": {
@@ -128,6 +110,57 @@ To configure a dual package setup, you can follow these steps:
128110

129111
The `default` field is the fallback entry point for both conditions. It's used for the actual JS code when the library is imported or required.
130112

113+
3. Optionally change the `main` field in your `package.json` to point to the CommonJS build:
114+
115+
```diff
116+
- "main": "./lib/module/index.js",
117+
+ "main": "./lib/commonjs/index.js",
118+
```
119+
120+
This is needed if you want to support tools that don't support the `exports` field and need to use the CommonJS build.
121+
122+
4. Optionally add a `module` field in your `package.json` to point to the ESM build:
123+
124+
```diff
125+
"main": "./lib/commonjs/index.js",
126+
+ "module": "./lib/module/index.js",
127+
```
128+
129+
The `module` field is a non-standard field that some tools use to determine the ESM entry point.
130+
131+
5. Optionally add a `types` field in your `package.json` to point to the CommonJS type definitions:
132+
133+
```diff
134+
"main": "./lib/commonjs/index.js",
135+
"module": "./lib/module/index.js",
136+
+ "types": "./lib/typescript/commonjs/src/index.d.ts",
137+
```
138+
139+
This is necessary to support legacy TypeScript setup, i.e. which have `moduleResolution: "node10"` or `moduleResolution: "node"` under the `compilerOptions` section in the `tsconfig.json`.
140+
141+
Putting it all together, the fields in your `package.json` file should look like this:
142+
143+
```json
144+
{
145+
"main": "./lib/commonjs/index.js",
146+
"module": "./lib/module/index.js",
147+
"types": "./lib/typescript/commonjs/src/index.d.ts",
148+
"exports": {
149+
".": {
150+
"import": {
151+
"types": "./lib/typescript/module/src/index.d.ts",
152+
"default": "./lib/module/index.js"
153+
},
154+
"require": {
155+
"types": "./lib/typescript/commonjs/src/index.d.ts",
156+
"default": "./lib/commonjs/index.js"
157+
}
158+
},
159+
"./package.json": "./package.json"
160+
}
161+
}
162+
```
163+
131164
> **Important:** With this approach, the ESM and CommonJS versions of the package are treated as separate modules by Node.js as they are different files, leading to [potential issues](https://nodejs.org/docs/latest-v19.x/api/packages.html#dual-package-hazard) if the package is both imported and required in the same runtime environment. If the package relies on any state that can cause issues if 2 separate instances are loaded, it's necessary to isolate the state into a separate CommonJS module that can be shared between the ESM and CommonJS builds.
132165
133166
## Guidelines

0 commit comments

Comments
 (0)