Site icon FSIBLOG

How to Fix the “SyntaxError: Cannot Use Import Statement Outside a Module” in Node.js

SyntaxError: Cannot Use Import Statement Outside a Module

SyntaxError: Cannot Use Import Statement Outside a Module

Have you ever started your Node.js app, only to see an error like SyntaxError: Cannot use import statement outside a module your heart sinks. You thought you were doing everything rightusing import, clean code, modular design. But instead you’re stuck. Don’t worry it’s not a mystical bug, and yes, you can fix it (and understand why it happened).

What on Behind the Scenes

Node.js supports two (or more) module systems, and your file is being treated as not a module that allows import statements. Specifically:

Fix it in a Node.js Project

Here I walk you through a typical Node.js project, how to decide what module system you want, and how to implement it.

Decide your module system

When you start your project (or inherit one), you should ask:

If you choose CommonJS (stay with require)

If you’re staying with CommonJS:

If you choose ES Modules (use import/export)

If you want to use import and export, here’s how to set up your Node.js project:

  1. In your package.json (root of project) add:
{
  "type": "module",
  ...
}
  1. This tells Node: treat .js files as ESM by default.
  2. If you want to use .js files as CommonJS in a mixed setup, you can add "type": "module" and use .cjs extension for CommonJS files, or leave .js as CommonJS and use .mjs for ESM. Many posts mention this but don’t show examples. For instance:
    • utils.mjs → ESM
    • legacy.cjs → CommonJS
  3. Rename files if needed: use .mjs extension for modules if you don’t want to change package.json or if you have older setup.
  4. Ensure Node version supports ESM (prefer Node 14+; ESM got more stable in Node 16+).
  5. Use import { something } from './module.js'; (note the ./module.js includes extension unless using package resolution).
  6. No mixing of require() and import in the same file unless you handle dynamic imports or carefully manage interoperability.
  7. If using older modules (CommonJS only) inside ESM files, you can use dynamic import:
const mod = await import('./commonjs-module.cjs');
  1. or use the createRequire helper:
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const oldMod = require('./oldFile.js');

Debugging the “Cannot use import statement outside a module” Error

When you still hit the error despite your setup, try these checks:

Migrating a small Node.js project to ESM

Let’s say you have a small project with files: index.js, utils.js. You want to use import syntax going forward. Steps:

In package.json, add "type": "module".

Update utils.js:

// utils.js
export function greet(name) {
  return `Hello, ${name}!`;
}

Update index.js:

import { greet } from './utils.js';
console.log(greet('World'));

Edge Cases and Bonus Tips

Fix Decision Matrix

Your situationWhat to do
New project, you want import/export syntaxUse ESM: set "type": "module" in package.json, use .js or .mjs, no mixing
Legacy project with many require(), don’t want big changes nowStay CommonJS: use require(), ensure "type" is not set to "module", convert gradually
Mixed modules (some old, some new)Use .cjs for old CommonJS, .mjs for new ESM; or set "type": "module" and convert old files carefully; use dynamic import for tricky cases
Unexpected “Cannot use import statement outside a module” errorDebug: check Node version, check "type" in package.json, check file extension, ensure correct syntax and file path, check test/bundler config

Final Thoughts

The error SyntaxError: Cannot use import statement outside a module may look scary, but it’s really a signal telling you: “Node is treating this file as non-module (CommonJS) but you used ES module syntax (import)”. Once you understand the distinction between CommonJS and ES Modules, and how Node decides which one you’re using (via package.json "type" or file extensions like .mjs/.cjs), it becomes straightforward.

Exit mobile version