HOW TO FIX SyntaxError: Cannot use import statement outside a module
Vložit
- čas přidán 8. 08. 2022
- Are you using imports in a node.js application and running into the SyntaxError: Cannot use import statement outside a module error?
Here are 3 ways to fix this error.
If you're developing a JavaScript application, then add "type": "module" into your package.json file.
If you're developing a TypeScript application, then make sure "module" is set to "commonjs" in your tsconfig.json file.
If you're importing a script in a HTML file, then add type="module" into the script tag.
Have you run into this error? We all have! Leave a comment if this helped you solve the issue in your project.
Hey, what if it doesn't fix the issue?
I'm using Js, and I already had the "type":"module" set inside my package.json
What other reason might there be for the issue to occur? :(
@@adrian-ioanmunteanu3027 samee, I'm facing that error right now😢
thansk for give me this information i have try to fix that error from previous 5 days i search more but finfally recive your vidoe and i have fixed it
Bro I am sick getting this error finally you helped me thanx
Thank you so much for your simple solution💙
The best you really helped me ❤
Thanks man, It worked!
Hi, i am creating a widget that is heavily dependent on firebase. I put the firebase initialize command in a separate js file from the main js file.
For some reason, anytime i run the local host it throws this exact error. even after inputing the "type"="module" type="module" in json and html respectively, i am still getting the error. Has anyone found another solution please?
i ran into this another errror: Failed to resolve module specifier "mysql2". Relative references must start with either "/", "./", or "../". How can i fix it, i am trying to connect to sql database for my project. Thanks
Thank you! I added Jest and couldn't get it to run. Adding "type" into package.json fixed it! I didn't find this in the Jest documentation..
shout out to you , you solve my problem😍
I have created a new file Header.js .... created a function header.. exported it .... imported inside the app.js...... but unable to use it as a tag in app.js ... like how r we supposed to use it in react...
Hi,
I am still facing issues after this fix. Forced clean cache too. Working on .ts. target and module set inside compiler options as per the video but still. Any ideas?
How did you fix it? I still get the error.
what about "SyntaxError: Identifier 'sequelize' has already been declared" ?
It is woking, but if import is inside called function, then it is giving error.
not even using nodejs and encoounter this, pls be specific on the title!
and what do you do if you dont have the node file
im new to this shit how do i create the module
What I dont get, is why doesnt it install the module/cjs type from the get go? ITs the stupidest thing
great video bro !!!!
Thanks!
its not working
thanks alot
bullshit