A Gentle Introduction to ESModules in Node.js

October 8, 2021

At the end of the April Node.js v10 reached "end of life". This means that this version will no longer be maintained and will not be patched with fixes for bugs or known security vulnerabilities. (Check more on Node.js Releases page). This also means that every supported version of Node.js JavaScript runtime supports Node's official EcmaScript modules. This brings some controversy because some people would prefer to stick with CommonJS in the Node ecosystem. Nevertheless, ESModules are now a reality, so let's learn how to use them in a Node.js environment.

First of all, we need a JavaScript we want to run:

add.js
export function add(a, b) {
return a + b
}

Next, we want to import that JavaScript file to the main index.js file.

index.js
import { add } from './add.js'
console.log(add(2, 2))

Excellent, now let's run it with node ./index.js or, for simplicity node .

terminal
node .
(node:3106) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/lukaspolak/Documents/esmodules/index.js:1
import { add } from './add.js'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47

Using ESModules is an opt-in feature. You can either enable it by renaming file extensions from .js to .mjs, or you can opt-in entire directory by adding "type": "module" to package.json file.

First, let's try to rename files to enable usage of ESModules. Do not forget to rename the import.

add.mjs
export function add(a, b) {
return a + b
}
index.mjs
import { add } from './add.mjs'
console.log(add(2, 2))
terminal
node ./index.mjs
4

Awesome! It is working now. Now let's rename files back to the .js extension, and let's add "type": "module" to package.json file.

package.json
{
"name": "esmodules",
"version": "0.0.1",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Lukas Polak <lukash.polak@gmail.com> (https://lukaspolak.com/)",
"license": "MIT"
}
terminal
node ./index.js
4

That's it! It is pretty easy to run ESModules in Node.js. There is a lot more to talk about ESModules, you can read the official Announcing core Node.js support for ECMAScript modules article or dig deep to official documentation.

Share this post on Twitter