ts-node won't resolve unpostfixed file names
Search Terms
file extension resolve
Expected Behavior
npx ts-node --esm test should run the test.ts file.
Actual Behavior
Cannot find module '/<root>/test/test'
Steps to reproduce the problem
- Create a file
test.tsand put anything in it. Or nothing. - Run
npx ts-node --esm test
Minimal reproduction
See above.
Specifications
- ts-node version: v10.9.1
- node version: v19.3.0
- TypeScript version: tsc version 4.9.4
- tsconfig.json, if you're using one:
{
"compilerOptions": {
"target": "es2021",
"lib": [
"es2021"
] ,
"jsx": "react" ,
/* Modules */
"module": "es2022" ,
"moduleResolution": "node",
"types": [
"@cloudflare/workers-types",
"jest"
] ,
"resolveJsonModule": true ,
/* JavaScript Support */
"allowJs": true ,
"checkJs": false ,
"noEmit": true ,
/* Interop Constraints */
"allowSyntheticDefaultImports": true ,
"forceConsistentCasingInFileNames": true ,
/* Type Checking */
"strict": true,
/* Completeness */
"skipLibCheck": true
}
}
- package.json:
{"type": "module"}
- Operating system and version: Windows 10
- If Windows, are you using WSL or WSL2?: WSL2
I'm seeing similar behavior for imports as well. I have a file wtf.ts with the contents:
import { test } from "./test";
console.log(test);
and test.ts contains
export const test = "???";
And running this with npx ts-node --esm -T wtf.ts gives me CustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts.
However if I add a .ts extension to the import, it works (it prints out "???" as I would expect):
import { test } from "./test.ts";
console.log(test);
But then my IDE gives me typescript warnings that the .ts shouldn't be there. Well, I agree.
I'm seeing similar behavior for imports as well. I have a file
wtf.tswith the contents:import { test } from "./test"; console.log(test);and
test.tscontainsexport const test = "???";And running this with
npx ts-node --esm -T wtf.tsgives meCustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts.However if I add a
.tsextension to the import, it works (it prints out "???" as I would expect):import { test } from "./test.ts"; console.log(test);But then my IDE gives me typescript warnings that the
.tsshouldn't be there. Well, I agree.
same issue here
Same issue here
I'm seeing similar behavior for imports as well. I have a file
wtf.tswith the contents:import { test } from "./test"; console.log(test);and
test.tscontainsexport const test = "???";And running this with
npx ts-node --esm -T wtf.tsgives meCustomError: Cannot find module '/<root>/test/test' imported from /<root>/test/wtf.ts. However if I add a.tsextension to the import, it works (it prints out "???" as I would expect):import { test } from "./test.ts"; console.log(test);But then my IDE gives me typescript warnings that the
.tsshouldn't be there. Well, I agree.same issue here
setting "experimentalSpecifierResolution": "node" will solve the issue.
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"target": "ES2017",
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true
},
"ts-node": {
"experimentalSpecifierResolution": "node",
"pretty": true,
"transpileOnly": true,
"esm": true
}
}