Modules & npm/package.json

Node.js Fundamentals — Modules & npm/package.json
Node.js Fundamentals
Course 1 · Chapter 2 · Modules & npm/package.json

📦 Modules & npm/package.json

Node.js applications are built from modules — reusable pieces of code. The module system lets you organize code, avoid global scope pollution, and share code between files. npm (Node Package Manager) lets you download and manage packages (libraries) that others have published. The package.json file is the heart of dependency management. This chapter covers how to create and use modules, and how to manage packages with npm.

🧩 The Module System: CommonJS

Node.js uses CommonJS modules. A module is just a JavaScript file that exports code for other files to use.

// math.js — Define a module function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } // Export the functions module.exports = { add, subtract };
// app.js — Use the module const math = require('./math'); console.log(math.add(5, 3)); // 8 console.log(math.subtract(5, 3)); // 2

module.exports

What a module shares. Can be a function, object, class, or anything else.

require()

Load a module. Returns what the module exported.

Local Modules

require('./path') — load a file in your project.

Installed Packages

require('lodash') — load an npm package.

📥 npm: Package Manager

npm is Node's package manager. It lets you:

  • Download and install packages (libraries) that others have written
  • Manage versions of packages your project depends on
  • Publish your own packages for others to use
  • Run scripts defined in package.json

Installing a Package

$ npm install lodash This: 1. Downloads lodash from npmjs.com 2. Stores it in node_modules/ folder 3. Updates package.json and package-lock.json Then use it: const _ = require('lodash'); console.log(_.shuffle([1, 2, 3, 4, 5]));

📄 package.json: Your Project's Config

package.json is a JSON file that describes your project and its dependencies.

{ "name": "my-awesome-app", "version": "1.0.0", "description": "My first Node.js app", "main": "app.js", "scripts": { "start": "node app.js", "test": "jest", "dev": "nodemon app.js" }, "dependencies": { "express": "^4.18.0", "lodash": "^4.17.21" }, "devDependencies": { "nodemon": "^2.0.20", "jest": "^29.0.0" }, "author": "Your Name", "license": "MIT" }

name & version

Package identity. Follows semantic versioning (1.0.0 = major.minor.patch).

scripts

Commands you can run: npm start, npm test, npm run dev.

dependencies

Packages your app needs to run (installed in production).

devDependencies

Packages only needed for development (testing, linting, bundling).

🆕 Creating package.json

Interactive Setup

$ npm init Prompts for: name, version, description, entry point, test command, etc. Creates package.json with your answers.

Quick Setup (use defaults)

$ npm init -y Creates package.json with default values (faster).

🔢 Semantic Versioning (Semver)

Versions follow the pattern MAJOR.MINOR.PATCH (e.g., 1.5.3):

  • 1.0.0 — Breaking changes (incompatible API)
  • 1.5.0 — New features (backward compatible)
  • 1.5.3 — Bug fixes (backward compatible)

Version Constraints in package.json

Exact Version
"lodash": "4.17.21" // Installs exactly 4.17.21 // No updates, even if 4.17.22 exists
Flexible Version
"lodash": "^4.17.21" // Allows 4.17.21 to 4.99.99 // ^ = compatible minor/patch // ~ = only patch updates

💻 Coding Challenges

Challenge 1: Create & Use Modules

Create a utils.js module that exports functions for validation (email, phone number). Create app.js that requires and uses these functions.

Goal: Practice module creation and require().

→ Solution

Challenge 2: npm & package.json

Initialize a new Node.js project with npm init. Install lodash and express. Add a custom npm script that echoes "Hello from npm script". Run it.

Goal: Get comfortable with npm and package.json.

→ Solution

Challenge 3: Exporting Different Types

Create modules that export: a function, an object, a class, and a constant. From app.js, require each and use them in different ways.

Goal: Understand that module.exports can be anything.

→ Solution

💡 node_modules/ is Big

After you npm install packages, a huge node_modules/ folder is created. Don't commit this to git — add it to .gitignore. When cloning a repo, just run npm install and it recreates node_modules from package.json. This keeps your repo small and reproducible.

🎯 What's Next

Now that you can organize code with modules and manage packages with npm, we'll explore Callbacks & Promises — diving deeper into async patterns beyond setTimeout.