Cannot Use Import Statement Outside a Module in NodeJS

Cannot Use Import Statement Outside a Module in NodeJS

“Cannot use import statement outside a module” is a common error in a NodeJS application while working with JavaScript or TypeScript. I faced this issue in the Node.js application for both JS and TS. Here, I will tell you its main causes and most possible ways to resolve it. 

This error usually appears when Node.js does not know how to treat your JavaScript file. By default, Node.js assumes files are written using the older CommonJS format. When you try to use the import keyword without telling Node.js to enable ES module support, it throws this error. This is common in production environments, CI pipelines, and even local setups when configuration files are missing or mismatched.

In modern Node.js versions used across the tech industry, ES modules are fully supported, but they must be enabled correctly. Most issues happen when file extensions, package.json settings, or the Node.js version do not align with each other. Once Node.js understands whether your project is using CommonJS or ES modules, this error disappears completely.

// Check how Node.js is interpreting your project
console.log("Module type:", process.env.npm_package_type || "commonjs");

This quick check helps confirm whether Node.js is treating your project as an ES module or a CommonJS module.

For both JS and TS, there are different solutions for this error. I will explain all of them, let’s start.

Update (2026): This article has been refreshed to include modern Node.js ES module behavior, current TypeScript configuration, and fixes that work with Node.js 18 and newer versions commonly used in production today.

Cannot use import statement outside a module

cannot use import statement outside a module

First of all check whether you are using the latest version (or >= 13.2.0) of Node.js or not. If not, try updating node.js version and then try. If you are already using the latest version, then try the following solutions. 

For JavaScript (if Node.js application is in JS)

For JavaScript, try the following solutions:

Solution 1

Sometimes this issue is related to the extensions .js and .mjs. To resolve it, you need to add the type property in the package.json file. In the top level, add this field as:

{

  "name": "app-name",

  "version": "1.1.1.1",

  "type": "module",

  "scripts": {},

   ...

}

After adding this field, .js and .mjs files will be interpreted as ES modules. Also it can be done by interpreting the individual files as CommonJS by using .cjs extension, see solution 2.

Solution 2

In this solution, you will need to rename all of the files with extension .mjs. Now you will not need to add type property in the package.json file. 

Alternative Solution

In this solution, you need to change .js files to .mjs.

import { createRequire } from 'module'
const require = createRequire(import.meta.url);

and easily use the ‘require’ as usual.

Cannot use import statement outside a module – for TypeScript

Once I faced this issue in TypeScript also, to resolve it here you need to edit the tsconfig.json file. Just replace the property

"module": "value"

with

"module": "commonjs". 

Note: for babel-node you need to change the –extensions option like babel-node --extensions \".ts,.tsx\" src/index.

Error in Browser for JavaScript

When you get this issue (“cannot use import statement outside a module”) in the browser console (in case of ES6), you will need to do some minor changes to get rid of it.

<body>

    <script type="module" src="index.js"></script>

 </body>

This way all of the JS files will be loaded as the type of ‘module’. This is also a common issue when you are working with ES6 and importing the JS file with type mismatch.

Frequently Asked Questions

Does this error happen only in older Node.js versions?

No, this error can happen even in modern Node.js versions like Node 18 or Node 20. The issue is not the version itself, but how the project is configured. If Node.js does not know that your files should be treated as ES modules, it will throw this error regardless of the version.

Can I use both import and require in the same Node.js project?

Yes, but you need to be careful. Node.js treats import and require differently. In ES module projects, you can still use require by creating it manually using the module utility. Mixing them without proper configuration is a common reason this error appears.

import { createRequire } from "module";
const require = createRequire(import.meta.url);

Why does this error appear during deployment but not locally?

This often happens because the production environment is different from local setup. The Node.js version, package.json configuration, or build process may not match. Many developers in the US face this issue during CI or cloud deployments where defaults are stricter.

Is using “type”: “module” always the best solution?

Not always. While it works well for ES module-based projects, some older libraries still expect CommonJS. If you are working on an existing project, using .cjs or keeping CommonJS may be safer. The best choice depends on your project setup and dependencies.

Can this error occur in frontend JavaScript as well?

Yes, this error can also appear in the browser if scripts are not loaded as modules. When using modern JavaScript with import statements, the browser must know that the file is a module. Otherwise, it will throw the same error in the console.

<script type="module" src="index.js"></script>

Lastly,

Here I have written the most effective and common solutions for this error. In most of the cases you will definitely resolve this issue with one of the solutions provided here.

In some cases these solutions may not work and I need more information about the error for resolution. In these cases you are welcome to use the comment box to elaborate your problem, I will give you the solution as soon as possible.

So this is the solution on the basis of my understanding of the issue, also I have gone through some of the online solutions from other developers. You are also welcome to share your views on this issue, and give any alternative solution if you have. It will help the developers. Also feel free to contact me. 

Leave a Reply

Your email address will not be published. Required fields are marked *