Node.js ES Modules (ECMAScript Modules)

Subject: Node.js

What Are ES Modules?\n- export: Share functions, variables, or classes from a file.\n- import: Bring shared code into another module.\n- Native to browsers and now fully supported in Node.js (v12+ experimental, v16+ stable).\n\n---\n\n### Enabling ES Modules in Node.js\n1. Use .mjs Extension\nbash\nnode myModule.mjs\n\n2. Use "type": "module" in package.json\njson\n{\n \"type\": \"module\"\n}\n\nThis lets you use .js files with ESM syntax.\n\n---\n\n### Example of ES Module Usage\n#### math.js\njs\nexport function add(a, b) {\n return a + b;\n}\n\nexport function subtract(a, b) {\n return a - b;\n}\n\n#### app.js\njs\nimport { add, subtract } from './math.js';\n\nconsole.log(add(5, 3)); // Output: 8\nconsole.log(subtract(10, 4)); // Output: 6\n\nNote: This requires the file extension to be .mjs, or your package.json must include \"type\": \"module\".\n\n---\n\n### Default Exports in ES Modules\n#### logger.js\njs\nexport default function log(message) {\n console.log(\"Log:\", message);\n}\n\n#### app.js\njs\nimport log from './logger.js';\nlog(\"This is an ES Module example\");\n\n\n---\n\n### Top-Level await\nES Modules support await at the top level without needing to wrap code in an async function:\njs\n// top-level-await.js\nconst res = await fetch('https://jsonplaceholder.typicode.com/posts/1');\nconst data = await res.json();\nconsole.log(data);\n\nMust be run with \"type\": \"module\" in package.json.\n\n---\n\n### Differences Between CommonJS and ES Modules\n| Feature | CommonJS (require) | ES Modules (import) |\n|------------------------|----------------------------|-------------------------------|\n| Import Syntax | const fs = require('fs') | import fs from 'fs' |\n| Export Syntax | module.exports = ... | export default / export {}|\n| Top-Level await | Not supported | Supported |\n| File Extension | .js (default) | .mjs or .js with type |\n| Static Analysis | Not supported | Supported |\n\n---\n\n### Mixing CommonJS and ES Modules\n- You can import CommonJS modules into ES Modules using import.\n- You cannot use require() inside ES Modules.\n- To import an ES Module into CommonJS, use dynamic import() which returns a Promise.\n\n#### Example: Importing CommonJS into an ES Module\njs\n// app.mjs\nimport fs from 'fs';\n\nfs.readFile('example.txt', 'utf8', (err, data) => {\n if (err) throw err;\n console.log(data);\n});\n\n\n---\n\n### When to Use ES Modules\n- When building modern apps with consistent frontend/backend syntax.\n- When using tools like Babel, Webpack, or Rollup that prefer ESM.\n- When you need top-level await, static imports, or tree shaking.\n\n---\n\n### Key Takeaways\n- ES Modules are now fully supported in Node.js.\n- Enable using .mjs or \"type\": \"module\" in package.json.\n- Use import and export instead of require() and module.exports.\n- Avoid mixing module types unless necessary.\n- Some npm packages may still use CommonJS, so test compatibility when mixing systems.

Next : Node.js Modules