Skip to content

Fixing "Must use import to load ES Module" in Node.js

The error "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module" occurs when Node.js attempts to load an ES module using CommonJS syntax, or when there's a mismatch between your project's module system configuration and how you're trying to import/export files.

Understanding the Problem

Node.js supports two module systems:

  • CommonJS: Uses require() and module.exports (traditional Node.js modules)
  • ES Modules (ESM): Uses import and export statements (standard JavaScript modules)

When you see this error, it typically means:

  1. You're trying to use ES module syntax without proper configuration
  2. There's a version compatibility issue
  3. You're mixing module systems incorrectly

Primary Solutions

1. Add "type": "module" to package.json

The most common solution is to explicitly declare your project as using ES modules:

json
{
  "name": "your-project",
  "version": "1.0.0",
  "type": "module",
  // other configurations...
}

With this setting, Node.js will treat all .js files as ES modules, allowing you to use import/export syntax.

2. Use .mjs File Extension

Alternatively, you can rename your ES module files with the .mjs extension:

bash
mv hello.js hello.mjs

Then update your import statement:

js
import { myArr } from './hello.mjs';
console.log(myArr);

TIP

The .mjs extension explicitly tells Node.js to treat the file as an ES module, regardless of package.json configuration.

3. Ensure Node.js Version Compatibility

ES module support has evolved across Node.js versions:

  • Node.js 12+: Experimental support with flags
  • Node.js 14+: Stable support (recommended)
  • Node.js 16+: Full, mature support

Check your Node.js version:

bash
node --version

If you're using Node.js < 14, you'll need the experimental flag:

bash
node --experimental-modules index.js

WARNING

For production applications, always use Node.js 14 or higher to avoid experimental features.

Common Scenarios and Fixes

Third-party Package Compatibility

Some packages (like node-fetch v3+) are ESM-only. If you encounter this error with a specific package:

bash
# For node-fetch, you can downgrade to CommonJS-compatible version
npm install node-fetch@2

Or configure your project to fully support ES modules as described above.

Version Mismatch Issues

If you switch Node.js versions or collaborate with others:

bash
# Clear node_modules and reinstall dependencies
rm -rf node_modules package-lock.json
npm install

This ensures all dependencies are built for your current Node.js version.

Babel Alternative (Legacy Approach)

For older projects or specific tooling requirements, you can use Babel to transpile ES modules:

bash
npm install --save-dev @babel/node @babel/core @babel/preset-env

Create a .babelrc file:

json
{
  "presets": ["@babel/preset-env"]
}

Run your code with:

bash
npx babel-node index.js

DANGER

babel-node is suitable for development but not production. For production, pre-compile your code with Babel first.

Best Practices

  1. Consistent Module System: Stick to either ES modules or CommonJS throughout your project
  2. Node.js Version: Use Node.js 14+ for production applications
  3. Explicit Extension: Use .mjs for ES modules and .cjs for CommonJS when mixing systems
  4. Package.json Configuration: Always declare "type": "module" for ES module projects

Troubleshooting Checklist

  • ✅ Check Node.js version (14+ recommended)
  • ✅ Add "type": "module" to package.json for ES modules
  • ✅ Use consistent import/export syntax throughout project
  • ✅ Clear node_modules and package-lock.json when switching Node versions
  • ✅ Verify third-party packages support your module system

By following these guidelines, you can resolve the "Must use import to load ES Module" error and properly configure your Node.js project for ES module support.