-
Notifications
You must be signed in to change notification settings - Fork 123
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
ES Module error: Cannot use import statement outside a module #265
Comments
I got the same error. In my case, the issue has been resolved by changing the option in from: As mentioned in TypeStrong/ts-node#922 (comment)
|
When you set the Node.js version: 16.4.0 |
This is a major blocking issue for us, and we're considering switching away from ts-node-dev (which worked perfectly in the past) as many dependencies are switching to ESM. We are using: In In And we are getting: |
Same issue here. This needs to be updated. |
Same issue |
Any update to this? The issue is still persistent in the package. |
I also encounter this problem, so when will ESM be supported? |
Yep, same issue. I'm giving up on react / typescript. It's a nightmare. |
A workaround is to override compiler options for ts-node and keep TS compiling to module type of choice Can be done within // tsconfig.json
{
"compilerOptions": {
"module": "es2020",
// ... rest of config
},
"ts-node": {
"compilerOptions": {
"module": "commonjs"
}
}
} ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.2) |
I still get issues when using ESM only modules with the above config from @johanolandero. Is there any progress on |
ESM is being a nightmare to me. The solution of @johanolandero doesn't work if you are using |
I have the same problem when trying to run mocha tests with ts-node |
Did anybody find a solution to this mess? |
If your compile time is quick you can instruct nodemon to restart your app using the ts-node ESM loader: nodemon --exec 'node --loader ts-node/esm' src/index.ts Otherwise, you can use "scripts": {
"server:build:dev": "tsc -w",
"server:watch": "watchexec --postpone --restart --debounce 250 --no-vcs-ignore --filter 'dist/**/*' 'node dist/index.js'",
"server:dev": "npm-run-all --parallel server:build:dev server:watch",
} |
This doesn't help since that's for ts-node options not for ts-node-dev, the issue still persists even today for me, can't get top-level to work properly sadly (works perfectly with ts-node, the fact is that ts-node-dev doesn't do that), waiting for an update that will allow us the save behaviour in tsconfig.json as ts-node provides already. Will probably try to figure this out more or simply change to something like nodemon that's already mentioned. |
I managed to get everything working with Nodemon + Babel + Babel TypeScript plugins/presets. And I think the solution also involves ts-node, can't remember now. Yeah confirmed it, it uses ts-node: I hope this gets solved soon as I want to keep using ts-node-dev but I need ESM and top level await and all the good stuff from EcmaScript Next. UpdateI've been lately using the following approach which is fast and smooth: |
SolutionTo use the In {
- "type": "commonjs"
+ "type": "module"
} In {
"compilerOptions": {
- "module": "CommonJS"
+ "module": "ESNext" // or ES2015, ES2020
},
"ts-node": {
+ "esm": true
}
}
|
@pravesa Unfortunately throws an error: |
@carlocorradini This error might be caused while resolving your imported module path. So, could you provide your exact error stack |
Sadly this doesn't work for me.I have everything configured correctly, type module in package.json, target and module to ESNext in tsconfig, moduleResolution to node, esModuleInterop true, allowSyntheticImports true, and Then in my scripts: And I get the error that I posted many times in this repository:
With nodemon and ts-node I got it working this way: No issues so far. |
Same error as @angelhdzmultimedia |
@carlocorradini I hope that the error message you get in your terminal is like this, So, to resolve this I need your exact error stack |
@carlocorradini example directory
NOTE -->
|
Hi @angelhdzmultimedia, I like your theme, is it applied to IDE only or desktop in general? what theme are you using (for VSCode and OS)? is it on a Linux distro? |
Same |
Issue description
I am looking at porting some projects to ESM and I don't know what I am doing wrong. This is the full error:
Context
OS version (is it docker or host?), ts-node-dev version Windows 10 Home
Node version 16.2.0
Did you try to run with ts-node?
Yes, this is what I tried:
npx ts-node main.ts
returned:However
node --loader ts-node/esm main.ts
worked.Did you try to run with
--files
option enabled?Yes, no changes
Did you try to run with
--debug
option enabled?Yes, nothing special
Do you have a repro example (git repo) with simple steps to reproduce your problem?
Yes, here: https://github.com/puchm/ts-node-dev-esm-test
Just run
npm start
The text was updated successfully, but these errors were encountered: