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()andmodule.exports(traditional Node.js modules) - ES Modules (ESM): Uses
importandexportstatements (standard JavaScript modules)
When you see this error, it typically means:
- You're trying to use ES module syntax without proper configuration
- There's a version compatibility issue
- 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:
{
"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:
mv hello.js hello.mjsThen update your import statement:
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:
node --versionIf you're using Node.js < 14, you'll need the experimental flag:
node --experimental-modules index.jsWARNING
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:
# For node-fetch, you can downgrade to CommonJS-compatible version
npm install node-fetch@2Or configure your project to fully support ES modules as described above.
Version Mismatch Issues
If you switch Node.js versions or collaborate with others:
# Clear node_modules and reinstall dependencies
rm -rf node_modules package-lock.json
npm installThis 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:
npm install --save-dev @babel/node @babel/core @babel/preset-envCreate a .babelrc file:
{
"presets": ["@babel/preset-env"]
}Run your code with:
npx babel-node index.jsDANGER
babel-node is suitable for development but not production. For production, pre-compile your code with Babel first.
Best Practices
- Consistent Module System: Stick to either ES modules or CommonJS throughout your project
- Node.js Version: Use Node.js 14+ for production applications
- Explicit Extension: Use
.mjsfor ES modules and.cjsfor CommonJS when mixing systems - 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.