Submitting the form below will ensure a prompt response from us.
One of the most common JavaScript and Node.js errors developers encounter is:
Cannot use import statement outside a module
This error usually appears when working with ES Modules, Node.js applications, TypeScript projects, or frontend JavaScript code.
So, what causes this error, and how can you fix it?
The error occurs when JavaScript encounters an import statement in a file that is not being treated as an ES Module.
For example:
import express from "express";
If the runtime expects CommonJS syntax instead of ES Modules, it throws:
SyntaxError: Cannot use import statement outside a module
JavaScript supports two module systems:
Traditionally used in Node.js.
Example:
const express = require("express");
Modern JavaScript module system.
Example:
import express from "express";
The error occurs when these systems are mixed incorrectly.
In Node.js, files are treated as CommonJS by default.
If you’re using import, add the following to your package.json:
{
"type": "module"
}
Example
{
"name": "my-app",
"version": "1.0.0",
"type": "module"
}
After adding this, Node.js will recognize ES module syntax.
Another solution is using the .mjs extension.
Example:
app.mjs
Then:
import express from "express";
Node.js automatically treats .mjs files as ES Modules.
Sometimes developers copy browser-based JavaScript into Node.js projects.
Example:
import { myFunction } from "./utils.js";
Without proper module configuration, Node.js cannot process the import statement.
TypeScript projects may generate this error if module settings are incorrect.
Incorrect tsconfig.json
{
"compilerOptions": {
"module": "commonjs"
}
}
Correct Configuration
{
"compilerOptions": {
"module": "esnext"
}
}
This enables ES module support.
You Might Also Like:
import fs from "fs";
console.log("Hello");
SyntaxError: Cannot use import statement outside a module
Add:
{
"type": "module"
}
to package.json.
| Feature | Import | Require |
|---|---|---|
| Module System | ES Modules | CommonJS |
| Syntax | Modern | Traditional |
| Static Analysis | Yes | Limited |
| Tree Shaking | Supported | Not Supported |
If your project uses CommonJS:
const express = require("express");
instead of:
import express from "express";
Add:
{
"type": "module"
}
and keep:
import express from “express”;
Older environments may require transpilation.
Install:
npm install @babel/core @babel/node @babel/preset-env
Babel converts modern JavaScript into compatible code.
Python also supports imports:
import os
print(os.getcwd())
However, Python’s module system works differently and does not require ES Module configuration.
const express = require("express");
import fs from "fs";
Avoid mixing module systems unless necessary.
ES Modules often require explicit extensions:
import helper from "./helper.js";
not:
import helper from "./helper";
Forgetting to include “type”: “module” is one of the most common causes.
Suppose you’re building a Node.js API:
import express from "express";
const app = express();
app.listen(3000);
Without proper module configuration, the application fails to start.
Adding:
{
"type": "module"
}
resolves the issue.
Modern frameworks and runtimes increasingly prefer ES Modules, including:
As a result, understanding module configuration has become essential for JavaScript developers.
Resolve JavaScript Errors Faster
Get expert help troubleshooting Node.js and frontend development issues.
The “Cannot Use Import Statement Outside a Module” error occurs when JavaScript encounters an ES Module import statement in a file that is being treated as a CommonJS module.
The most common fixes include:
Understanding the difference between ES Modules and CommonJS helps prevent this error and ensures smoother JavaScript development.