Open luyifo opened 5 months ago
I had the same issue, downgrade Node to 18.18 help. My engines:
"engines": {
"node": ">=18.18.0 <18.19.0"
},
Have you solved this problem?I switched the versions of 22,18 and 16 with different problems, and the configuration file has been modified, but it still doesn't work
"module": "CommonJS", "esModuleInterop": true,
My code compilation is no problem, I specially configured a file for "ts-node", code compilation is another file, anyway just to see the state of writing code, so I specially configured a file for "ts-node", compilation is another file;"tsconfig.dev.json"and"tsconfig.json"
"dev": "cross-env TS_NODE_PROJECT=tsconfig.dev.json nodemon --exec ts-node src/main.ts",
"dev": "cross-env TS_NODE_PROJECT=tsconfig.dev.json nodemon --exec ts-node src/main.ts",
seems to work for me with node and --loader
flag
{
"scripts": {
- "not-work": "ts-node-esm src/main.ts",
+ "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
}
}
node: v20.5.0 ts-node: v10.9.2
seems to work for me with node and
--loader
flag{ "scripts": { - "not-work": "ts-node-esm src/main.ts", + "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } }
node: v20.5.0 ts-node: v10.9.2
This worked for me. Thanks @lhapaipai
try to use tsx
instead. It works flawlessly for me.
seems to work for me with node and
--loader
flag{ "scripts": { - "not-work": "ts-node-esm src/main.ts", + "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } }
node: v20.5.0 ts-node: v10.9.2
worked for me too thanks
Change package.json type from "module" to "commonjs" helped to me. https://www.npmjs.com/package/ts-node#commonjs
seems to work for me with node and
--loader
flag{ "scripts": { - "not-work": "ts-node-esm src/main.ts", + "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } }
node: v20.5.0 ts-node: v10.9.2
Thanks @lhapaipai!
In my scenario, I had to also workaround the following error. I think it stems from my project does not have a top-level tsconfig.json
, but rather sub-folders define different configs similar to a monorepo. So I either had to manually cd
down into a sub-folder then run the command, or I could have node figure it out via the import command.
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined in ES module scope
My modified command is:
{
"scripts": {
- "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
+ "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning -e \"import('${0}');\""
}
}
Example Usage:
yarn dev path/to/my/file.ts
seems to work for me with node and
--loader
flag{ "scripts": { - "not-work": "ts-node-esm src/main.ts", + "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } }
node: v20.5.0 ts-node: v10.9.2
Thanks @lhapaipai!
In my scenario, I had to also workaround the following error. I think it stems from my project does not have a top-level
tsconfig.json
, but rather sub-folders define different configs similar to a monorepo. So I either had to manuallycd
down into a sub-folder then run the command, or I could have node figure it out via the import command.Object.defineProperty(exports, "__esModule", { value: true }); ^ ReferenceError: exports is not defined in ES module scope
My modified command is:
{ "scripts": { - "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" + "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning -e \"import('${0}');\"" } }
Example Usage:
yarn dev path/to/my/file.ts
Awesome, that solved my problem. Thanks!
According to the warnings, I've also added a workaround for the "experimentalWarning" issue. I created a "loader.js" in my project root's folder:
import { register } from "node:module";
import { pathToFileURL } from "node:url";
// Register the TypeScript loader
register("ts-node/esm", pathToFileURL('./'));
Then I changed my package.json to:
{
"scripts": {
"dev": "node --import './loader.js' src/index.ts"
}
}
It runs when I change the type to commonjs in package.json.
Search Terms
When the node version is 18.20.0, running TS-node --esm **.ts displays TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts". Runs successfully after the Node version is reduced to 16.20.2.
Expected Behavior
Actual Behavior
Steps to reproduce the problem
Minimal reproduction
Specifications