Node.js ES Modules

Summary: in this tutorial, you’ll learn how to use ES modules in Node.js via the import and export statements.

JavaScript uses the ECMAScript module (or ES module) to create packages for reuse. Besides the CommonJS module, Node.js supports ES modules in version 14.0.0.

So Node.js has two types of modules: ES modules and CommonJS modules.

By default, Node.js treats JavaScript as CommonJS modules. But you can tell Node.js to treat JavaScript code as ES modules.

Node.js will treat the following files as ES modules:

  • Files ending in .mjs
  • Files ending in .js when the nearest parent package.json file contains a top-level field "type" with a value of "module".
  • Pass the argument --eval, or pied to the node command via STDIN with flag --input-type=module.

Node.js ES module via .mjs files

First, create a new file called math.mjs and add the following code:

function add(x,y) {
    return x + y;
}

function subtract(x,y){
    return x - y;
}

export { add, subtract};Code language: JavaScript (javascript)

In the math module:

  • First, define the add and subtract functions.
  • Second, export the add and subtract function using the export statement.

Second, create a new file called app.mjs that uses the math module:

import { add, subtract } from './math.mjs';

let result = add(20, 10);
console.log(result);

result = subtract(30, 10);
console.log(result);Code language: JavaScript (javascript)

In the app.mjs file:

  • First, import the add and substract functions from the math module using the import statement.
  • Second, call the add and subtract functions.

Third, run the app.mjs file using the node command:

node app.mjsCode language: CSS (css)

And you should see the following in the console:

{ result: 30 }
{ result: 20 }Code language: CSS (css)

In this example, we use the files that end in .mjs, node.js treats all of them as ES modules.

Node.js ES module with type module in package.json

First, rename the math.mjs and app.mjs to math.js and app.js.

Second, create a package.json file with the top-level field type with the value "module":

{
    "type":"module"
}Code language: JSON / JSON with Comments (json)

Third, run the app.js file using the node command:

node app.jsCode language: CSS (css)

And you should see the same output as shown in the first example.

Node.js ES module with the eval argument

The following example runs the node command with the eval argument and the --input-type=module:

node --input-type=module --eval "import { delimiter } from 'path'; console.log(delimiter);"Code language: JavaScript (javascript)

In this example, we pass the following code to the --eval argument:

import { delimiter } from 'path';console.log(delimiter);Code language: JavaScript (javascript)

This imports the delimiter from the path module and outputs it to the console.

Likewise, you can use the following command:

echo "import { delimiter } from 'path'; console.log(delimiter);" | node --input-type=moduleCode language: JavaScript (javascript)

Summary

  • Node.js supports both CommonJS modules and ES modules
  • By default, Node.js treats files as CommonJS modules.
  • Use .mjs or a package.json with the top-level field type="module" if you want Node.js to treat files as ES modules.
Was this tutorial helpful ?