-
-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ts-node doesn't run ESM modules as expected, either refusingimport
statements in ts file or not being able to run .ts
files
#2086
Comments
I've got the same issue. It looks like this problem has been around for a few years from the posts on github and stackoverlow. |
When I run
tsconfig: "compilerOptions": {
"target": "es2016",
"module": "esnext",
"esModuleInterop": true,
} |
I had the same issue on a computer with macOS, another with Windows, and another with Ubuntu, all of them running Node version 20. When testing the previous version of Node (18.18.2) on a machine with Windows and another with Ubuntu, it worked on both. It appears that the issue arises when using version 10 of ts-node with Node version 20. |
I was finally able to get this working with Node 20 and ts-node 10 by setting Provided your project can support your Typescript compiling to CommonJS, it works well. |
In the latest node.js weekly newsletter they sent an interesting gist / project stub addressing the issues - it may be helpful! https://gist.github.com/khalidx/1c670478427cc0691bda00a80208c8cc It is really weird that ts-node in conjuction with Node 20 is making such problems! Also.. have you tried tsx? |
This is probably a duplicate of #1997? |
Hey, I had the same issue for my use case that resembles your "Steps to reproduce problem". My case: Configuring SyntaxError: Cannot use import statement outside a module by pointing at the ESM Solution: register ts-node in the environments where the processes are executed.My setup:
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"rootDir": "./src" /* Specify the root folder within your source files. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"exclude": ["node_modules", ...]
}
There are 2 approaches you can take: A. Set the args to the child proc as workerProcess = fork(testProcessPath, {
stdio: [0, "pipe", "pipe", "ipc"],
execArgv: ["-r", "ts-node/register"],
...
}); B. Add those arguments in the test script in "scripts": {
...
"test": "set NODE_OPTIONS=-r ts-node/register && jest --watchAll --no-cache",
}, Solutions found from this thread #619 and the docs. Hope that helps! |
Why are we still doing commonjs? Is ESM not the standard at this point? |
I came across this very issue a few days ago, but it was incoherent over different repositories. I finally concluded that this error started with Typescript 5.6.x, and doesn't happen in 5.5.x and below (this is without any tsconfig.json in place, which I expect should work out of the box). Not sure what changed in typescript 5.6 which makes the inferred tsconfig to start failing like this (this happens in both Node 18 and Node 20 btw, which I know have other issues). |
having the same problem. The solutions from the readme doesn't work :( |
Same issue on Mac with node 20.15.1 |
Search Terms
ESM import paths
Expected Behavior
I expect
ts-node
to be able to run typescript code using ESM module imports. However , it either fails withor after adding a
"type": "module"
topackage.json
, fails withThe documentation is rather insufficient on how ts-node operations with tsconfig.json and the
type
value in package.json.Of all of
target
,module
andmoduleResolution
intsconfig.json:compilerOptions
, what should be the first value that is set (as in, doestarget
determinemodule
andmoduleResolution
's values, or vice versa ?)Please provide some guidance on how this works.
Actual Behavior
Steps to reproduce the problem
This file creates basically parses the env variables and then creates a bunch of worker processes.
run with
npx ts-node workers/master.ts
Minimal reproduction
Specifications
OS: macOS Sonoma 14.1.1
"ts-node": "^10.9.1"
node
version: 20.6.1tsconfig.json
The text was updated successfully, but these errors were encountered: