When your JavaScript runtime throws an uncaught syntaxerror: cannot use import statement outside a module, it usually means the environment does not recognize your file as an ES module.

Why does this uncaught syntaxerror happen

Browsers and Node treat files differently depending on configuration and file extension. If you use an import or export statement in a script that the platform loads as a classic script, the engine can only parse traditional CommonJS or plain script syntax, so it throws an uncaught syntaxerror: cannot use import statement outside a module.

In Node, prior to certain recent defaults, .js files were interpreted as CommonJS unless they were placed inside a package.json with type set to module or used the .mjs extension. In the browser, a script tag without type="module" also runs in legacy mode, so dynamic import and top-level import are not allowed, leading to the same uncaught syntaxerror: cannot use import statement outside a module message.

Cannot use import statement outside module in JavaScript | bobbyhadz
Cannot use import statement outside module in JavaScript | bobbyhadz

How to recognize the exact error context

You will often see the error stack pointing to the line with import, and the console will clearly label it as an uncaught syntaxerror: cannot use import statement outside a module. The message may include hints about the script being treated as classic or strict mode, which helps you narrow down whether the issue is in the browser or in Node.

Sometimes bundlers like webpack or Vite rewrite imports in ways that obscure the original source, but the underlying cause is still that the runtime environment expected a module format and did not receive one. Learning to read the line number and the resource path in the console is a quick way to confirm whether the error originates from a mismatched script tag or an incorrect module setting in your build tool.

Browser fixes for module syntax errors

In HTML, change the script tag to include type="module" so the browser knows it should handle ES module syntax. Once the browser treats the file as a true module, import and export statements are parsed correctly and the uncaught syntaxerror no longer appears.

Three.js で Uncaught SyntaxError: Cannot use import statement outside a ...
Three.js で Uncaught SyntaxError: Cannot use import statement outside a ...
  • Use <script type="module" src="app.js"></script> for browser entry points.
  • Ensure your server serves .js files with correct MIME types, as some configurations can cause scripts to be interpreted as plain text.
  • Avoid mixing nomodule and module scripts unless you have a clear fallback strategy, since older browsers may handle them in ways that lead to confusing errors.

Additionally, check for typos in the path inside import statements, because a broken URL can cause the browser to fetch the wrong resource and apply a different execution context, indirectly triggering the same uncaught syntaxerror: cannot use import statement outside a module.

Node.js configuration and package rules

In Node, you can align your project with ES modules by setting "type": "module" in package.json, renaming files to use the .js extension with type module semantics, or using the .mjs extension directly. Once the runtime recognizes the file as a module, import and export statements work as expected and the uncaught syntaxerror disappears.

If you maintain legacy CommonJS code, you can keep .cjs extensions for those entry points while using .js with type module for modern syntax. This selective approach prevents accidental mixing of module systems that often leads to uncaught syntaxerror: cannot use import statement outside a module deep in the dependency tree.

Uncaught SyntaxError: Cannot use import statement outside a module ...
Uncaught SyntaxError: Cannot use import statement outside a module ...

Tooling, bundlers, and transpiler considerations

Bundlers like webpack, Rollup, and Vite can transform module syntax so your final bundle runs in environments that do not support native ES modules. When developing, you might still see warnings or uncaught syntaxerror: cannot use import statement outside a module if the dev server misclassifies a file or serves it with the wrong headers.

Check your bundler configuration for module rules, target environments, and output format. Ensure that your dev server and production builds treat entry files consistently, and verify that any dynamic import() calls are not being rewritten into a context where the runtime cannot resolve them as modules.

Best practices to prevent future issues

Adopt a consistent module strategy early in the project, documenting whether you use ES modules natively, a bundler, or a transpiler. Clear conventions for file extensions, package.json settings, and script tags reduce the likelihood of seeing uncaught syntaxerror: cannot use import statement outside a module during development or in production.

NodeJS : Uncaught SyntaxError: Cannot use import statement outside a ...
NodeJS : Uncaught SyntaxError: Cannot use import statement outside a ...

Regularly test your code in the target environments, keep Node versions up to date, and use automated checks in your build pipeline to flag mismatched syntax before deployment. These habits make it easier to spot subtle configuration drift and keep your modules loading smoothly.

Understanding why the engine reports uncaught syntaxerror: cannot use import statement outside a module gives you direct control over module resolution, script loading, and build tooling, so your JavaScript runs reliably across browsers and servers.