Modules & npm/package.json
๐ฆ Modules & npm/package.json
๐งฉ 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().
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.
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.
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.