← Back to Tutorials Chapter 3

Modules & Package Management

CommonJS Modules

CommonJS is the original module system in Node.js. It uses require() to import modules and module.exports to export them.

// math.js (export)
function add(a, b) { return a + b; }
function subtract(a, b) { return a - b; }
module.exports = { add, subtract };

// app.js (import)
const math = require('./math');
console.log(math.add(5, 3)); // 8
console.log(math.subtract(10, 4)); // 6
Caching: Modules are cached after the first require() call. Subsequent calls return the same instance.

ES Modules

ES Modules (ESM) are the official JavaScript standard. Use import and export with a "type": "module" in package.json or the .mjs extension.

// utils.mjs (export)
export function greet(name) {
  return `Hello, ${name}!`;
}
export const PI = 3.14159;

// main.mjs (import)
import { greet, PI } from './utils.mjs';
console.log(greet('Node'));
console.log('PI:', PI);

package.json Configuration for ESM

{
  "name": "my-app",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

NPM Basics

NPM (Node Package Manager) is the default package manager for Node.js. It's used to install, manage, and share packages.

# Initialize a new project (creates package.json)
npm init
npm init -y   # Skip prompts

# Install packages
npm install express
npm install --save-dev nodemon
npm install lodash@4.17.21  # Specific version

# Install globally
npm install -g typescript

# Uninstall packages
npm uninstall lodash

# List installed packages
npm list --depth=0

Package.json in Detail

The package.json file is the heart of any Node.js project. It contains metadata, dependencies, scripts, and configuration.

{
  "name": "my-awesome-app",
  "version": "2.1.0",
  "description": "A sample Node.js application",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "nodemon src/index.js",
    "test": "jest",
    "build": "tsc",
    "lint": "eslint ."
  },
  "dependencies": {
    "express": "^4.18.0",
    "mongoose": "^7.0.0"
  },
  "devDependencies": {
    "nodemon": "^3.0.0",
    "jest": "^29.0.0",
    "eslint": "^8.0.0"
  },
  "author": "Your Name",
  "license": "MIT"
}

NPM Scripts

NPM scripts run commands defined in package.json. They're a convenient way to automate common tasks.

npm run dev     # Run "dev" script
npm run test    # Run "test" script
npm start       # Runs "start" script
npm test        # Runs "test" script
npm run build   # Run "build" script
Pre/Post Hooks: NPM automatically runs pre and post scripts if defined. For example, prestart runs before start.

Dependency Management

Semantic Versioning (SemVer)

NPM uses semantic versioning: MAJOR.MINOR.PATCH

Publishing Packages

# Login to NPM
npm login

# Build your package
npm run build

# Publish
npm publish

# Update version
npm version patch  # 1.0.0 -> 1.0.1
npm version minor  # 1.0.1 -> 1.1.0
npm version major  # 1.1.0 -> 2.0.0
Exercise: Create a new Node.js project, install the chalk package (for colored terminal output), and write a script that prints "Hello, NPM!" in green.