📦Can't use import statements (module settings)
SyntaxError: Cannot use import statement outside a module
The file uses the modern 'import' style (ES modules), but the environment running it is reading it the old way (require).
🤔 Why does it happen?
- package.json doesn't have the "type": "module" setting
- Old-style and new-style files are mixed together
- The file extensions (.js/.mjs/.cjs) don't match the settings
🔧 Try this
- Decide which style the project uses and make everything match
- Showing the AI your package.json together with the error speeds things up
🤖 Ask your AI like this
I get the 'Cannot use import statement outside a module' error below. Please check the package.json settings and file extensions and make the project use one module style. [Paste the full error here]
Replace the [ ] parts with your own situation, and paste the error with a few extra lines before and after it.
Related terms
Other errors in this group
- Module not found
Module not found: Can't resolve - Command not found
command not found - Package version conflict (ERESOLVE)
npm ERR! ERESOLVE unable to resolve dependency tree - Port already in use
EADDRINUSE: address already in use - Types don't match (TypeScript)
Type 'X' is not assignable to type 'Y' - No permission, so install failed (EACCES)
EACCES: permission denied - Node version doesn't match
The engine "node" is incompatible with this module - Out of memory (heap out of memory)
JavaScript heap out of memory - Build failed on code checks (ESLint)
Failed to compile — ESLint errors - File or folder not found (ENOENT)
ENOENT: no such file or directory