Photo by Chris Ried on Unsplash
How to fix the 'Cannot use import statement outside a module' error in Node JS
I got this error twice while working on a project this week so I wanted to write a quick guide in case I forget how to fix it again.
The "SyntaxError: Cannot use import statement outside a module" error comes up when you try to use an import statement and you are not using an ES module.
To resolve this error, you have to add this to your package.json file:
"type": "module"
Once you add this to your package.json file, this error should be resolved.
Thank you for reading this! I'm over at @tbrmonster on Twitter if you'd like to talk about Node js or coding things.