Search Terms
ts-node, ERR_MODULE_NOT_FOUND
Expected Behavior
run my typescript es module files.
Actual Behavior
I have a project that looks like this
- package.json
- tsconfig.json
- src/main.ts
- src/module.ts
I have a "start" script in my package.json that I run via pnpm start that executes
ts-node ./src/main.ts. (I have also tried this with npm and it also didn't work)
When I run this I get the error "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.". So I do as I'm told and I add it to my package.json and run it again.
Now I get this error: "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [path-to-project]/src/main.ts"
I manage to resolve this error by adding "--esm" to my start script like this: ts-node --esm ./src/main.ts. If I run my start script I get a new error: "Cannot find module '[path-to-project]/src/module' imported from [path-to-project]/src/main.ts"
I can surpass this error by doing this:
// main.ts
// --
import { myLog } from "./module"
// --
// ++
// ** Add a ts-ignore because typescript doesn't like me adding the file extension. */
// @ts-ignore
import { myLog } from "./module.ts"
// ++
Now my code runs but I have some issues with this
- I don't like adding ".ts" behind every import. This defeats the purpose of auto-imports
- Typescript really doesn't like me adding a file extension in an import statement.
How can I make my code work with ts-node by removing the ".ts" extension in the import statement?
Minimal reproduction
// main.ts
import { myLog } from "./module"
console.log("hiii")
myLog()
// module.ts
export const myLog = () => console.log("module imported")
Specifications
- ts-node version: 10.8.0 | 10.7.0 (tried both versions)
- node version: 16.15.0 | 18.2.0 (switched using nvm)
- TypeScript version: 4.7.2
- tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"emitDecoratorMetadata": true,
"strict": false,
"resolveJsonModule": true,
"typeRoots": ["src/types", "./node_modules/@types"],
"baseUrl": ".",
"paths": {
"@entities": ["src/entities/index.ts"],
"@entities/*": ["src/entities/*"],
"@config/*": ["src/config/*"],
"@helpers/*": ["src/helpers/*"],
"@mytypes/*": ["src/types/*"],
"@server/*": ["src/server/*"]
}
},
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"]
}
{
"type": "module",
"devDependencies": {
"@types/node": "^17.0.36",
"ts-node": "10.7.0",
"typescript": "^4.7.2"
},
"scripts": {
"start": "ts-node --esm ./src/main.ts"
}
}
- Operating system and version: MacOS
Search Terms
ts-node, ERR_MODULE_NOT_FOUND
Expected Behavior
run my typescript es module files.
Actual Behavior
I have a project that looks like this
I have a "start" script in my package.json that I run via
pnpm startthat executests-node ./src/main.ts. (I have also tried this with npm and it also didn't work)When I run this I get the error "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.". So I do as I'm told and I add it to my package.json and run it again.
Now I get this error: "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for [path-to-project]/src/main.ts"
I manage to resolve this error by adding "--esm" to my start script like this:
ts-node --esm ./src/main.ts. If I run my start script I get a new error: "Cannot find module '[path-to-project]/src/module' imported from [path-to-project]/src/main.ts"I can surpass this error by doing this:
Now my code runs but I have some issues with this
How can I make my code work with ts-node by removing the ".ts" extension in the import statement?
Minimal reproduction
Specifications
{ "compilerOptions": { "target": "ESNext", "module": "ESNext", "skipLibCheck": true, "sourceMap": true, "outDir": "./dist", "moduleResolution": "node", "removeComments": true, "noImplicitAny": false, "strictNullChecks": true, "strictFunctionTypes": true, "noImplicitThis": true, "noUnusedLocals": false, "noUnusedParameters": false, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "allowSyntheticDefaultImports": true, "esModuleInterop": true, "experimentalDecorators": true, "allowJs": true, "emitDecoratorMetadata": true, "strict": false, "resolveJsonModule": true, "typeRoots": ["src/types", "./node_modules/@types"], "baseUrl": ".", "paths": { "@entities": ["src/entities/index.ts"], "@entities/*": ["src/entities/*"], "@config/*": ["src/config/*"], "@helpers/*": ["src/helpers/*"], "@mytypes/*": ["src/types/*"], "@server/*": ["src/server/*"] } }, "exclude": ["node_modules"], "include": ["./src/**/*.ts"] }{ "type": "module", "devDependencies": { "@types/node": "^17.0.36", "ts-node": "10.7.0", "typescript": "^4.7.2" }, "scripts": { "start": "ts-node --esm ./src/main.ts" } }