SyntaxError: Cannot use import statement outside a module
SyntaxError: Cannot use import statement outside a module in JavaScript means you used import in a file that is being run as an old-style script. Node and browsers only allow import inside files they treat as ES modules.
How do I fix SyntaxError: Cannot use import statement outside a module?
SyntaxError: Cannot use import statement outside a module means the file is running as a classic script or CommonJS, where import is not allowed. In Node, set "type": "module" in package.json, since npm init writes "commonjs", or rename the file to .mjs. In a browser, add type="module" to the <script> tag.
- 1. Check package.json typeIn Node, look for "type" in package.json. "commonjs" or a .cjs file means import is not allowed.
- 2. Check the script tagIn the browser, write <script type="module" src="main.js">, and open the page through a local server.
- 3. Do not mix systemsUse either import and export, or require() and module.exports, in one file. Not both.
What does SyntaxError: Cannot use import statement outside a module mean?
JavaScript has two module systems. The modern one uses import and export (ES modules). The older Node one uses require() and module.exports (CommonJS). Node treats a .js file as CommonJS when package.json says "type": "commonjs" (npm init writes this by default) and for .cjs files. With no "type" at all, Node 22 and newer spot the import and run the file as a module with a warning; older Node throws this error. A browser treats a <script> as a classic script unless it has type="module". In a classic script or CommonJS file, import is a syntax error.
Why does SyntaxError: Cannot use import statement outside a module happen?
1. A Node project set to CommonJS
npm init writes "type": "commonjs", so Node reads .js files as CommonJS. Change it to "module".
Breaks
// package.json
{ "name": "app", "type": "commonjs" }
// index.js
import fs from "fs";Works
// package.json
{ "name": "app", "type": "module" }
// index.js
import fs from "fs";2. A browser script tag without type="module"
Classic scripts cannot use import.
Breaks
<script src="main.js"></script>Works
<script type="module" src="main.js"></script>3. Staying with CommonJS instead
If you cannot change the project setting, use require, which CommonJS understands.
Breaks
import express from "express";Works
const express = require("express");How to find it in your own code
Checklist: 1) In Node, open package.json and look for "type". Set it to "module" to use import, or rename the file to .mjs. 2) In the browser, add type="module" to the <script> tag (and open the page through a local server, not as a file:// link). 3) Do not mix import and require in the same file. 4) If the error comes from a test runner, check its docs for ES module support.
Practise scope and closure bugs
None of the challenges raises this exact error yet. These practise the same kind of bug.
- Finding the first negative number crashesmedium
- describe(false) can't see the global labelhard
- Object freeze is shallow and allows nested settings mutationmedium
- Counter method crashes inside forEachhard
- Array fill with empty array shares same sublist instancemedium
- Adding a fee crashes before the fee existsmedium
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.