What is the difference between CommonJS and ES modules in Node?

Node.js basics · Freshers and experienced

Model answer

CommonJS uses require() and module.exports and loads files synchronously. ES modules use import and export, are the JavaScript standard, and support top-level await. Node uses ES modules when the file ends in .mjs or package.json has "type": "module".

Mixing them has rules, so pick one style per project.

Learn this topic

What Is Node.js? Install It and Run Your First Script in the course Node.js and Express: Build a Back End API from Scratch.

Sign in to add your own answer.

More questions