To load an ES module, set "type" - "module" in JavaScript

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

To load an ES module, set "type" - "module" in JavaScript #

The "To load an ES module, set "type": "module" in package.json" error occurs when using the ES6 Modules syntax in a Node.js application without setting the type property to module in your package.json file. To solve the error, set the type property to module in your package.json file.

to load es module set type module in package json

Here's an example of how the error occurs.

index.js
// ⛔️ To load an ES module, set "type": "module" in // package.json import moment from 'moment'; moment().format();

To be able to load ES modules, set the type property to module in your project's package.json file.

package.json
{ "type": "module", // 👇️ rest ... }

If your project doesn't have a package.json file, open your terminal in the project's root directory and run the following command.

shell
# only do this if you don't have a package.json file npm init -y

Now you can use the ES6 module import and export syntax in our Node.js code.

index.js
import moment from 'moment'; console.log(moment().format()); // 👉️ 2021-12-08T11:08:...
If you don't want to change the contents of your package.json file, you can change the file's extension to .mjs instead of .js.

For example, a file called index.js would become index.mjs. The .mjs extension marks the file as a module.

index.mjs
import moment from 'moment'; console.log(moment().format()); // 👉️ 2021-12-08T11:08:...

The code above works even without setting the type property to module in your project's package.json file.

When working with modules, you can't use the require syntax in your code, you can only use the ES6 modules import/export syntax.

Here are examples of how to use ES6 modules in a Node.js application.

You can use default imports/exports with ES6 modules in the following way:

index.js
// 👇️ default export export default function sum(a, b) { return a + b; }

And import the function in the other file.

another-file.js
// 👇️ default import import sum from './index.js'; console.log(sum(5, 5)); // 👉️ 10

Notice that we do not use curly braces when working with default imports.

You can only have 1 default export per file.

Here's how you use named imports/exports.

index.js
// 👇️ named export export function sum(a, b) { return a + b; }

And now we use a named import in the other file.

another-file.js
// 👇️ named import import {sum} from './index.js'; console.log(sum(5, 5)); // 👉️ 10

Notice the curly braces, this is how we import a named export.

You have to be consistent with your imports and exports. Don't use curly braces when importing default exports and use curly braces when importing named exports.

You can also mix and match, here's an example.

index.js
// 👇️ named export export const num = 20; // 👇️ default export export default function sum(a, b) { return a + b; }

And here are the imports.

another-file.js
// 👇️ default and named imports import sum, {num} from './index.js'; console.log(sum(5, 5)); // 👉️ 10 console.log(num); // 👉️ 20

We used a default import to import the sum function and a named import to import the num variable.

Conclusion #

The "To load an ES module, set "type": "module" in package.json" error occurs when using the ES6 Modules syntax in a Node.js application without setting the type property to module in your package.json file. To solve the error, set the type property to module in your package.json file.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.