The warning (node:...) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension is a common message in Node.js. It occurs when you try to run a file that uses the modern ES Module (import/export) syntax, but your Node.js project is not configured to interpret the file as an ES module.
This guide will explain the fundamental difference between Node.js's module systems and show you the two standard ways to fix this warning.
The Core Problem: CommonJS vs. ES Modules in Node.js
Node.js has historically used a module system called CommonJS, which uses the require() and module.exports syntax. This is the default system.
However, the official standard for modules in JavaScript is now ES Modules, which uses the import and export syntax.
The warning message is Node.js telling you: "You've written your code using import/export, but I'm currently in CommonJS mode, which doesn't understand this syntax. You need to tell me to switch to ES Module mode."
Example of problem:
index.js:
// This is ES Module syntax.
import moment from 'moment';
console.log(moment().format());When you run this file in a default Node.js project, you will get the warning.
Error Output:
(node:...) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
...
SyntaxError: Cannot use import statement outside a moduleSolution 1 (Recommended for New Projects): Set "type": "module"
This is the most common and modern approach. By adding a single property to your project's package.json file, you tell Node.js to treat all .js files in that project as ES Modules.
Solution:
- If you don't have one, create a
package.jsonfile by runningnpm init -yin your project's root directory. - Open your
package.jsonfile and add the"type": "module"property.
package.json:
{
"name": "my-node-app",
"version": "1.0.0",
"type": "module"
}Solution 2: Use the .mjs File Extension
If you don't want to change your entire project to ES Modules, you can signal to Node.js that a specific file is an ES module by changing its extension from .js to .mjs.
Solution: simply rename your file.
index.js->index.mjs
// This file is now named `index.mjs`
import moment from 'moment';
console.log(moment().format());A Note on require vs. import
Once you configure your project to use ES Modules (either with "type": "module" or .mjs files), you must use the import/export syntax consistently. You can no longer use the CommonJS require() function to import modules.
- ES Modules:
import moment from 'moment'; - CommonJS:
const moment = require('moment');
Conclusion
The "To load an ES module, set "type": "module"..." warning is a clear indicator that you are using modern import/export syntax in a project that Node.js is treating as a traditional CommonJS project.
To solve it, you have two excellent options:
- Set
"type": "module"in yourpackage.json: This is the recommended best practice for modern Node.js projects, as it makes ES Modules the default for all.jsfiles. - Rename your file with a
.mjsextension: This is a great way to mark a single file as an ES Module without changing your whole project's configuration.
By explicitly telling Node.js which module system to use, you will resolve the warning and be able to use the modern, powerful ES Module syntax.