Skip to main content

How to Resolve the "ReferenceError: module is not defined in ES module scope" Error in JavaScript

The ReferenceError: module is not defined in ES module scope is a common error that occurs in modern JavaScript when you try to mix two different module systems: CommonJS (require/module.exports) and ES Modules (import/export).

This guide will explain the difference between these two systems, show you why this error happens, and teach you the correct way to fix it by consistently using the modern ES Module syntax.

The Core Problem: CommonJS vs. ES Modules

JavaScript has two primary module systems that you will encounter.

  • CommonJS (CJS): This is the original module system used by Node.js. It uses the require() function to import modules and the module.exports object to export them. It is synchronous and designed for server-side code.
  • ES Modules (ESM): This is the modern, standardized module system that is now built into both JavaScript and Node.js. It uses the import and export keywords. It is asynchronous and is the standard for both browsers and modern Node.js development.

The module object (and therefore module.exports) is a global variable that only exists in a CommonJS environment. It does not exist in an ES Module environment.

The Cause of the Error: Mixing Module Systems

The error "module is not defined" occurs when the JavaScript runtime is treating your file as an ES Module, but your code attempts to use the CommonJS module.exports syntax.

For example, when a file contains an import statement, the JavaScript runtime automatically treats that file as an ES Module.

// index.js

// This `import` statement tells the runtime this is an ES Module.
import _ from 'lodash';

function sum(a, b) {
return a + b;
}

// ⛔️ ReferenceError: module is not defined in ES module scope
// The `module` object does not exist in this context.
module.exports = {
sum,
};
note

Because the runtime is in "ES module scope," it does not create the module variable, so attempting to access module.exports results in a ReferenceError.

The Solution: Use ES Module export Syntax

The fix is to be consistent. If you are using import, you must also use export. You cannot mix and match within the same file.

Named Exports

This is the most common and recommended way to export members from a module. You can have multiple named exports per file.

Solution:

// index.js

import _ from 'lodash';

// ✅ Use a named export
export function sum(a, b) {
return a + b;
}

// You can export other members too
export const VERSION = '1.0';

You would then import these using a corresponding named import.

// another-file.js
import { sum, VERSION } from './index.js';

console.log(sum(10, 5)); // Output: 15
console.log(VERSION); // Output: 1.0

Default Exports

A module can also have one (and only one) default export.

// index.js
import _ from 'lodash';

// ✅ Use a default export
export default function sum(a, b) {
return a + b;
}

A default export is imported without curly braces.

// another-file.js
import mySumFunction from './index.js'; // You can name it anything

console.log(mySumFunction(10, 5)); // Output: 15

Enabling ES Module Scope in Your Project

To use the import/export syntax, you must explicitly tell your environment to treat your files as ES Modules.

In Node.js

Add "type": "module" to your package.json file. This tells Node.js to treat all .js files in your project as ES Modules by default.

// package.json
{
"name": "my-project",
"version": "1.0.0",
"type": "module",
"dependencies": { ... }
}

In the Browser

When including your script in an HTML file, add type="module" to the <script> tag.

<!-- This enables `import` and `export` in browser scripts -->
<script type="module" src="main.js"></script>

Conclusion

The "module is not defined" error is a clear signal of a module system mismatch. It is not a bug, but a feature that enforces the rules of the current module scope.

  • The error means your file is being treated as an ES Module (likely because it contains an import statement or is configured as such), but you are trying to use CommonJS syntax (module.exports).
  • The solution is to be consistent. Replace your module.exports statements with the modern export (or export default) syntax.
  • Ensure your project is correctly configured to use ES Modules by setting "type": "module" in your package.json (for Node.js) or by using <script type="module"> (for browsers).