I was trying to execute the .ts
file by ts-node and got the following error:
(node:38108) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.
SyntaxError: Cannot use import statement outside a module
In many posts, the specifying of "type": "module"
in the package.json is being represented as the working solution. If only it were that simple… Once do it, the above error will change to:
TypeError: Unknown file extension “.ts” for C:XXXSample.ts
Too strange for ts-node which working with “.ts” files, does not it?
I have no the unambiguous experimental data, but I suppose, the "type": "module"
could cause more troubles because most libraries has Common.js format, so I am trying to avoid it as possible. The rebuttals are welcome.
About tsconfig.json
First of all, the ts-node using the tsconfig.json
as default even the target file is not covered by include
option or files
option. In my case, the main tsconfig.json
is:
{
"compilerOptions": {
"target": "ES2022",
"moduleResolution": "Node",
"esModuleInterop": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"removeComments": true,
"outDir": "Distributable/",
"declaration": true
},
"include": [ "Source/**/*" ]
}
But for the file which I am trying execute, another tsconfing file is actual:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "ESNext"
},
"include": [ "Tests/**/*" ]
}
To use this file, the --project tsconfig.test.json
option required when run the ts-node.