You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/typescript.md
+42-3Lines changed: 42 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,12 +121,39 @@ export const config = {
121
121
"esModuleInterop": true
122
122
},
123
123
"ts-node": {
124
-
"esm": true,
125
-
"experimentalSpecifierResolution": "node"
124
+
"esm": true
126
125
}
127
126
}
128
127
```
129
128
129
+
**Required package.json:**
130
+
```json
131
+
{
132
+
"name": "your-project",
133
+
"version": "1.0.0"
134
+
// ⚠️ DO NOT include "type": "module" when using ts-node/esm
135
+
// ts-node/esm works with CommonJS-style loading
136
+
}
137
+
```
138
+
139
+
**⚠️ Important Notes:**
140
+
141
+
1.**Do not use `"type": "module"`** in your package.json when using `ts-node/esm`. The ts-node/esm loader uses CommonJS-style module loading internally and is incompatible with `"type": "module"`.
142
+
143
+
2.**Use `.js` extensions in imports**: When writing TypeScript code for ESM, you must use `.js` extensions in your imports, even when importing TypeScript files:
144
+
145
+
```typescript
146
+
// ❌ Wrong - will cause "Cannot find module" error
147
+
importloginPagefrom"./pages/Login"
148
+
149
+
// ✅ Correct - use .js extension
150
+
importloginPagefrom"./pages/Login.js"
151
+
```
152
+
153
+
TypeScript will automatically resolve `.js` imports to `.ts` files during type-checking. This is the standard approach for ESM + TypeScript projects as documented in the [TypeScript handbook](https://www.typescriptlang.org/docs/handbook/modules/theory.html#typescript-imitates-the-hosts-module-resolution-but-with-types).
154
+
155
+
3.**For `"type": "module"` projects, use `tsx` instead**: If you need `"type": "module"` in your package.json, use `tsx/cjs` which is designed to work in both CommonJS and ESM contexts.
156
+
130
157
### Full TypeScript Features in Tests
131
158
132
159
With tsx or ts-node/esm, you can use complete TypeScript syntax including imports, enums, interfaces, and types:
@@ -174,7 +201,19 @@ This means the TypeScript loader isn't configured. Make sure:
174
201
175
202
**Error: Module not found when importing from `.ts` files**
176
203
177
-
Make sure you're using a proper TypeScript loader (`tsx/cjs` or `ts-node/esm`).
204
+
When using `ts-node/esm` with ESM, you need to use `.js` extensions in imports:
205
+
206
+
```typescript
207
+
// This will cause an error in ESM mode:
208
+
importloginPagefrom"./pages/Login"
209
+
210
+
// Use .js extension instead:
211
+
importloginPagefrom"./pages/Login.js"
212
+
```
213
+
214
+
TypeScript will resolve the `.js` import to your `.ts` file during compilation. This is the standard behavior for ESM + TypeScript.
215
+
216
+
Alternatively, use `tsx/cjs` which doesn't require explicit extensions.
0 commit comments