Repository navigation
Ts-node module resolution is not respected #1858
Description
Search Terms
ts-node module type resolution package.json
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /...
Expected Behavior
when "moduleType" is specified in ts-config.json it should ignore the "type": "module" specified in package.json
tsconfig.json
"ts-node": {
"moduleTypes": {
"*": "commonjs"
}
},package.json
{
"name": "typescript-incorrect-behaviour",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
Actual Behavior
"type": "module", is respected when tsconfig.json should be respected.
Even passing "NODE_OPTIONS='--import-type=commonjs'" does not get respected.
throws
Debugger attached.
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ~/Desktop/typescript-incorrect-behaviour/src/index.ts
at new NodeError (node:internal/errors:372:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:76:11)
at defaultGetFormat (node:internal/modules/esm/get_format:118:38)
at defaultLoad (node:internal/modules/esm/load:21:20)
at ESMLoader.load (node:internal/modules/esm/loader:407:26)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
at new ModuleJob (node:internal/modules/esm/module_job:66:26)
at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
Steps to reproduce the problem
npm init
npm install typescript ts-node @types/node
tsc --init
create a src/index.ts with console.log("hello") inside
add "type": "module" to package.json
add "start": "ts-node src/index.ts" to package.json
npm start
Minimal reproduction
I have an example here.
https://github.com/lantos-lgtm/typescript-incorrect-behaviour
git clone https://github.com/lantos-lgtm/typescript-incorrect-behaviour
cd typescript-incorrect-behaviour
npm install
npm startSpecifications
ts-node v10.9.1
node v16.16.0
compiler v4.7.4
- tsconfig.json, if you're using one:
{
"ts-node": {
"moduleTypes": {
"*": "commonjs"
}
},
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
- package.json:
{
"name": "typescript-incorrect-behaviour",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"start": "ts-node src/index.ts",
"start_node_env_override": "ts-node NODE_OPTIONS='--import-type=commonjs' src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "*",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
}
}
- Operating system and version: macos 12.5
- If Windows, are you using WSL or WSL2?: none
Activity
cspotcode commentedon Aug 8, 2022
I double-checked the docs, looks like moduleTypes might be configured incorrectly:
https://typestrong.org/ts-node/docs/module-type-overrides
lantos1618 commentedon Aug 8, 2022
Just updated to cjs still same response