Summary: in this tutorial, you’ll learn how to use ES modules in Node.js via the import and export statements.
How to instruct Node.js to treat JavaScript files as ES modules
Node.js supports two types of modules:
- CommonJS
- ES modules (ESM) (supported in Node 14.0.0 or later).
By default, Node.js treats JavaScript files as CommonJS modules. However, you can tell Node.js to treat JavaScript files as ES modules using one of the following techniques:
- JavaScript files ending with
.mjs - JavaScript files ending with
.jsand the nearest parentpackage.jsonfile contains a top-level field"type"with a value of"module". - Passing the argument
--eval, or pied to the node command viaSTDINwith flag--input-type=module. This technique is rarely used in practice but it is available.
Node.js ES module via .mjs files
First, create a new file called math.mjs and add the following code:
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
export { add, subtract };Code language: JavaScript (javascript)In the math module:
- First, define the
addandsubtractfunctions. - Second, export the
addandsubtractfunction using theexportstatement. This is the named export.
Alternatively, you can place the export keyword before the function like this:
export function add(a, b) {
return a + b;
}
export function subtract(a, b) {
return a - b;
}Code language: JavaScript (javascript)Second, create a new file called app.mjs that uses the math module:
import { add, subtract } from './math.mjs';
let a = 20,
b = 5;
let result = add(a, b);
console.log(`${a} + ${b} = ${result}`);
result = subtract(a, b);
console.log(`${a} - ${b} = ${result}`);Code language: JavaScript (javascript)In the app.mjs file:
- First, import the
addandsubstractfunctions from themathmodule using theimportstatement. - Second, call the
addandsubtractfunctions.
Third, run the app.mjs file using the node command:
node app.mjsCode language: CSS (css)You should see the following in the console:
20 + 5 = 25
20 - 5 = 15In this example, we use the files that end in .mjs, Node.js treats all of them as ES modules.
Set type module in package.json file
First, rename the math.mjs and app.mjs to math.js and app.js respectively.
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)You should see the same output as shown in the first example.
Using the eval command line 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)Output:
;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
- Use JavaScript files with
.mjsextension to instruct Node.js to treat the JavaScript files as ES modules. - Set the
"type": "module"field in thepackage.jsonto also instruct Node.js to treat JavaScript files as ES modules.