The ESLint error “must use import to load ES module” typically occurs when using the CommonJS require()
syntax in a project configured for ECMAScript Modules (ESM). This error is important to address because it ensures compatibility with modern JavaScript standards, promoting better module management and interoperability in your codebase.
The ESLint error “Must use import to load ES Module” occurs when you try to use CommonJS require()
syntax in a project configured to use ES Modules. Here are the specific conditions that trigger this error:
package.json
file has "type": "module"
.require()
instead of import
.To fix this, ensure you use import
statements for module loading and update your ESLint configuration if necessary.
Here are the common causes of the ‘ESLint error: must use import to load ES module’ along with examples:
Using CommonJS syntax in an ES module environment:
require
with ES module syntax.// Incorrect
const express = require('express');
// Correct
import express from 'express';
Incorrect type
field in package.json
:
type
field is not set to module
for ES modules.// Incorrect
{
"type": "commonjs"
}
// Correct
{
"type": "module"
}
Using .js
extension without specifying module type:
package.json
or using .js
for ES modules.// Incorrect
import { myFunction } from './myModule.js';
// Correct
import { myFunction } from './myModule.mjs';
Outdated Node.js version:
// Incorrect
node app.js
// Correct (for Node.js 12.x)
node --experimental-modules app.mjs
Misconfigured TypeScript settings:
// Incorrect tsconfig.json
{
"compilerOptions": {
"module": "commonjs"
}
}
// Correct tsconfig.json
{
"compilerOptions": {
"module": "esnext",
"esModuleInterop": true
}
}
These are some common scenarios that lead to the ‘must use import to load ES module’ error.
Here’s a step-by-step guide to resolve the ‘ESLint error: must use import to load ES module’:
package.json
Ensure your package.json
file has the "type": "module"
field to indicate that you’re using ES modules.
{
"type": "module"
}
Add the @babel/eslint-parser
to your devDependencies
and configure ESLint to use it.
Install @babel/eslint-parser
:
npm install --save-dev @babel/eslint-parser
Update .eslintrc.js
:
module.exports = {
parser: '@babel/eslint-parser',
parserOptions: {
requireConfigFile: false,
},
// other configurations
};
Ensure your tsconfig.json
is set up to handle ES modules.
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "./dist"
}
}
Ensure your code uses ES module syntax.
Correct:
import express from 'express';
Incorrect:
const express = require('express');
For conditional imports, use dynamic imports.
if (condition) {
import('module-name').then(module => {
// Use the module
});
}
For Node.js versions 12.x to 13.x, you might need to run Node.js with the --experimental-modules
flag.
node --experimental-modules your-script.js
For Node.js 14.x and later, this is not necessary if "type": "module"
is set in package.json
.
By following these steps, you should be able to resolve the ‘ESLint error: must use import to load ES module’ issue effectively.
To avoid encountering the ‘ESLint error: must use import to load ES module’ in future projects, follow these best practices:
Use ES Module Syntax:
import
and export
instead of require
and module.exports
.// Correct
import express from 'express';
// Incorrect
const express = require('express');
Configure package.json
:
"type": "module"
in your package.json
to ensure Node.js treats .js
files as ES modules.{
"type": "module"
}
Update ESLint Configuration:
@babel/eslint-parser
to handle modern JavaScript syntax.{
"parser": "@babel/eslint-parser",
"parserOptions": {
"sourceType": "module"
}
}
Ensure Correct File Extensions:
.js
extension in your import statements.import { myFunction } from './myModule.js';
Node.js Version:
Third-Party Packages:
tsconfig.json
is set up for ES modules.{
"compilerOptions": {
"module": "esnext",
"target": "esnext",
"moduleResolution": "node",
"esModuleInterop": true
}
}
By following these practices, you can maintain a consistent coding standard and avoid module-related errors in your projects.
To avoid module-related errors and ensure a consistent coding standard, follow these key points:
import
and export
instead of require
and module.exports
."type": "module"
in your package.json
to ensure Node.js treats .js
files as ES modules.@babel/eslint-parser
..js
in import statements."module": "esnext"
and "target": "esnext"
in tsconfig.json
.By following these practices, you can maintain a consistent coding standard and avoid module-related errors in your projects.