CommonJS Modules
Node.js uses the CommonJS module system by default. Each file is treated as a separate module.
// math.js\nconst add = (a, b) => a + b;\nconst subtract = (a, b) => a - b;\n\nmodule.exports = { add, subtract };\n\n// app.js\nconst math = require('./math');\nconsole.log(math.add(5, 3)); // 8ES Modules (ESM)
Node.js also supports ES modules. Use .mjs extension or set "type": "module" in package.json.
// utils.mjs\nexport const greet = (name) => `Hello, ${name}!`;\n\n// main.mjs\nimport { greet } from './utils.mjs';\nconsole.log(greet('Node'));NPM Basics
npm init -y # Initialize a project\nnpm install express # Install a package\nnpm install -g nodemon # Install globally\nnpm update # Update all packages\nnpm uninstall axios # Remove a package