Labs ICT
โญ Pro Login

NPM and package.json

Managing dependencies like a pro.

NPM (Node Package Manager) is the world's largest software registry. It has over 2 million packages. If you need a library for almost anything, NPM has it.

Initializing a Project

mkdir my-project
cd my-project
npm init -y  # Creates package.json with defaults

The package.json file is your project's manifest. It tracks dependencies, scripts, and metadata.

Installing Packages

# Install a package (adds to dependencies)
npm install express

# Install a dev-only package
npm install --save-dev nodemon

# Install globally (CLI tools)
npm install -g typescript

# Install from package.json
npm install

package.json Structure

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "My awesome project",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^3.0.2"
  }
}

NPM Scripts

Run scripts with npm run scriptname:

npm run start    # node app.js
npm run dev      # nodemon app.js
npm run test     # jest

# Special scripts don't need "run"
npm start    # Same as npm run start
npm test     # Same as npm run test
Try it Yourself โ†’

Version Ranges

^4.18.2    // Any version >= 4.18.2 and < 5.0.0
~4.18.2    // Any version >= 4.18.2 and < 4.19.0
4.18.2     // Exactly this version
>4.18.0    // Greater than 4.18.0
latest     // Always the latest version

Pro Tip: Always commit your package.json to version control, but never commit node_modules. Add node_modules to your .gitignore. Anyone can restore it with npm install.

๐Ÿงช Quick Quiz

What does NPM stand for?