How I Started Using ES6 Modules in Node.js (2026 Update)
ES modules (ESM) are now a first-class way to write Node.js code. Node supports both CommonJS and ESM, and you can opt into ESM using file extensions or package.json settings. This update gives you two clear paths: JavaScript or TypeScript. Option A: JavaScript (ESM) 1. Create a project mkdir node-esm cd node-esm npm init -y 2. Tell Node to treat .js as ESM Add this to package.json: { "type": "module" } Node will treat .js files as ESM in this package scope. If you prefer, you can also use .mjs for ESM files and .cjs for CommonJS. When there are no explicit markers, Node inspects the source to decide whether a file is ESM or CommonJS. Use explicit markers for clarity. ...
