-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
SyntaxError: Cannot use import statement outside a module #922
Comments
You’re trying to execute use it as an ES2015 module, but node.js currently understands CommonJS. |
Added documentation: https://github.com/TypeStrong/ts-node#syntaxerror. |
Having the same problem even when using commonjs
My project is an expo react-native project, but that shouldn't make a difference. But the error does seem to come from a dependency:
|
Update 3: Since Node 13, you can use either the .mjs extension or set "type": "module" in your package.json. You don't need to use the Update 2: Since Node 12, you can use either the .mjs extension or set "type": "module" in your package.json. And you need to run node with the Update: In Node 9, it is enabled behind a flag, and uses the .mjs extension.
https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import?rq=1 |
Thanks, I got it fixed that way! |
I had the same problem when I started to used babel... But later, I had a solution... I haven't had the problem anymore so far... Currently, Node v12.14.1, "@babel/node": "^7.8.4", I use babel-node and nodemon to execute (node is fine as well..) package.json: launch.json: Of course, with babel-node, you also normally need and edit another file, such as babel.config.js/.babelrc file |
I had the same problem and somehow fixed it. I have written a post about this, hope this helps https://xperimentalhamid.com/how-do-i/fix-cannot-use-import-statement-outside-a-module/ |
.mjs extension worked for me, thank you!! |
i keep getting this error when i import react in node for some reason SyntaxError: Cannot use import statement outside a module |
Do u solve this? |
If its an express project make this the last line in your server.js or app.js file |
To load an ES module, set "type": "module" in the package.json or use the .mjs extension. |
"type": "module" works, but only works if you are using Node v13.9.0 or above. |
Add .babelrc to root folder
{
"presets": ["env", "stage-0"]
} my package.json
"scripts": {
"watch": "nodemon --exec babel-node src/index.js"
},
"dependencies": {
"dotenv": "^8.2.0",
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1",
"nodemon": "^2.0.3"
} |
When using a
|
In case you stumble upon this and don't want to change the In my case I had a vue project and I wanted to test some parts of it with mocha/chai. I created a Also as additional info |
@Dravere we also support overriding compilerOptions inside the same tsconfig, which will only apply to ts-node. So you can keep it all in a single tsconfig file if you want. {
"ts-node": {
// these options are overrides used only by ts-node
// same as our --compilerOptions flag and our TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"module": "esnext"
} |
@cspotcode Oh, that is amazing. Thanks for that hint. Learned a lot about Typescript and ts-node while trying to fix that problem. |
* This was required to run ava tests as .ts file - Fixes "SyntaxError: Cannot use import statement outside a module" * Found this as work around noted here - TypeStrong/ts-node#922 (comment)
I have the same problem |
Estou com este mesmo problema no nodejs. Alguém pode me ajudar, por favor? |
* This was required to run ava tests as .ts file - Fixes "SyntaxError: Cannot use import statement outside a module" * Found this as work around noted here - TypeStrong/ts-node#922 (comment)
* This was required to run ava tests as .ts file - Fixes "SyntaxError: Cannot use import statement outside a module" * Found this as work around noted here - TypeStrong/ts-node#922 (comment)
* This was required to run ava tests as .ts file - Fixes "SyntaxError: Cannot use import statement outside a module" * Found this as work around noted here - TypeStrong/ts-node#922 (comment)
I get
When I try this
|
@acomito the |
How am I supposed to use For eg. I got these two errors function run(): void { // <------- Colon (:) is causing SyntaxError: Unexpected token ':' and
// void is causing error Type annotations can only be used in TypeScript files.ts(8010)
// Some code
} |
this works! thanks a lot! |
I got the same error because I have imported a lib with syntax |
i got the same error because I forgot to set extname in file that I was Importing |
What if I can't go back to |
when I do this, I get error:
|
This is pretty hilarious:
|
I use
nextjs
use
ts-node server.ts
, BTWmy
tsconfig.json
fileWhy is this?
The text was updated successfully, but these errors were encountered: