Warning: To load an ES module, set "type" - "module" in JS

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# Warning: To load an ES module, set "type" - "module" in JS

The error "To load an ES module, set "type": "module" in package.json or use the .mjs extension" 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

shell
(node: 9374)Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. /home/bobbyhadz/Desktop/project/src/App.js:1

Here's an example of how the error occurs.

index.js
// โ›”๏ธ To load an ES module, set "type": "module" in package.json or use the .mjs extension import moment from 'moment'; moment().format();

# Set the type attribute to module in package.json

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 ... }

set type to module in package json

The code for this article is available on GitHub

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
# ๐Ÿ‘‡๏ธ initialize package.json file npm init -y

issue npm init y command

Now you can use the ES6 modules import/export syntax in your Node.js code.

index.js
import moment from 'moment'; console.log(moment().format()); // ๐Ÿ‘‰๏ธ 2023-07-26T20:13:26+03:00
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.

# Using the ES6 modules import/export syntax in your code

Here is an example of using a default export and import.

This is a file called index.js that exports a function using a default export.

index.js
// ๐Ÿ‘‡๏ธ default export export default function sum(a, b) { return a + b; }

And here's how we'd import the function into a file called another-file.js.

another-file.js
// ๐Ÿ‘‡๏ธ default import import sum from './index.js'; console.log(sum(5, 5)); // ๐Ÿ‘‰๏ธ 10
The code for this article is available on GitHub

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

You can only have 1 default export per file, however, you can have as many named exports as necessary.

Here is an example of using named exports.

This is a file called index.js that exports a function and a variable using named exports.

index.js
// ๐Ÿ‘‡๏ธ named export export function sum(a, b) { return a + b; } // ๐Ÿ‘‡๏ธ named export export const site = 'bobbyhadz.com';

And this is how we'd import the named exports into a file called another-file.js.

another-file.js
// ๐Ÿ‘‡๏ธ named imports import {sum, site} from './index.js'; console.log(sum(15, 20)); // ๐Ÿ‘‰๏ธ 35 console.log(site); // ๐Ÿ‘‰๏ธ bobbyhadz.com
The code for this article is available on GitHub

Notice that we use curly braces when importing named exports.

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 of a file that has a named and a default export.

index.js
// ๐Ÿ‘‡๏ธ named export export const num = 20; // ๐Ÿ‘‡๏ธ default export export default function sum(a, b) { return a + b; }

And here are the default and named imports in another-file.js.

another-file.js
// ๐Ÿ‘‡๏ธ default and named imports import sum, {num} from './index.js'; console.log(sum(5, 5)); // ๐Ÿ‘‰๏ธ 10 console.log(num); // ๐Ÿ‘‰๏ธ 20
The code for this article is available on GitHub

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

Here are some things to note when using the ES6 modules import/export syntax:

  • You can have a maximum of 1 default export per file but can have as many named exports as necessary.
  • You have to specify the extension of the file in your import statements, e.g. import {num} from './index.js.

An alternative to setting the type attribute to module is to use .mjs as the extension of your files (instead of .js).

# Alternatively, you can set the file's extension to .mjs

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()); // ๐Ÿ‘‰๏ธ 2023-07-26T20:13:26+03:00
The code for this article is available on GitHub

The code above works even without setting the type property to module in your project's 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.

Copyright ยฉ 2024 Borislav Hadzhiev