How to Use ES Modules in Node.js: A Practical Guide
ES modules are the modern standard. Here is how to use them in Node.js practically.
How to Use ES Modules in Node.js: A Practical Guide
ES modules are the modern JavaScript standard. Here is how to use them in Node.js practically.
Enabling ES Modules
Set 'type': 'module' in your package.json, or use the .mjs file extension. This tells Node.js to treat your files as ES modules instead of CommonJS.
Import and Export Syntax
Use export to share values: export function add() {} or export const x = 1. Use import to use them: import { add } from './utils.js'. Note the .js extension is required in Node.js ES modules.
Default vs Named Exports
export default function() {} for a single main export, imported as import anything from './file.js'. Named exports use export const x, imported as import { x } from './file.js'.
Top-Level Await
ES modules support top-level await, so you can use await at the top level without wrapping in an async function. This is a real advantage over CommonJS for startup async work.
Importing CommonJS Modules
You can require CommonJS modules from ES modules with import. The module.exports becomes the default export: import express from 'express'.
The Trade-offs
ES modules give you static analysis, tree-shaking, and modern syntax, but some npm packages and tools still assume CommonJS. Most modern packages support both, but check if your dependencies work with ES modules.
The Takeaway
Use ES modules in Node.js by setting 'type': 'module' or using .mjs, using export and import with .js extensions, leveraging top-level await, and importing CommonJS modules as default imports. Check that your dependencies support ES modules.
Set 'type': 'module' in your package.json to make all .js files ES modules, or use the .mjs file extension for individual files. This tells Node.js to treat your files as ES modules instead of CommonJS.
Use export to share values: 'export function add() {}' or 'export const x = 1'. Use import to use them: 'import { add } from "./utils.js"'. The .js extension is required in Node.js ES modules, unlike bundlers that infer it.
The ability to use await at the top level of an ES module without wrapping in an async function. This is a real advantage over CommonJS for startup async work like connecting to a database before the server starts.
Yes. The module.exports of a CommonJS module becomes the default import in an ES module. For example, 'import express from "express"' works because express uses module.exports.
You get static analysis, tree-shaking, modern syntax, and top-level await. The trade-off is that some npm packages and tools still assume CommonJS, though most modern packages support both. Check your dependencies before committing.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

