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.
What are the trade-offs of ES modules in Node.js?
More FAQs in How to Use ES Modules in Node.js: A Practical Guide
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.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.