agyanadda@gmail.com

Node.js npm and package.json

Node.js Basics 1 min read Updated Dec 3, 2026 Topic 4 of 30

npm (Node Package Manager) installs open-source packages from npmjs.com — over two million of them.

Start a project

npm init -y creates package.json, the file that describes your project: name, version, scripts and dependencies.

Install packages

  • npm install express — a dependency your app needs to run
  • npm install -D nodemon — a dev dependency (only for development)
  • npm install — install everything listed in package.json
  • npm uninstall express, npm update, npm outdated

Packages are saved in the node_modules folder. Never edit it and never commit it to Git — add it to .gitignore.

package-lock.json

Records the exact versions installed, so everyone gets the same versions. Do commit this file. Use npm ci on servers for a clean, exact install.

Scripts

Add commands in "scripts" and run them with npm run <name> (npm start and npm test work without run).

npx

npx <tool> runs a package without installing it globally, e.g. npx create-vite@latest.

Versions

^4.21.0 allows minor updates (4.x), ~4.21.0 allows only patch updates (4.21.x).

Example

Example — A typical package.json JSON
{
  "name": "my-api",
  "version": "1.0.0",
  "type": "module",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "node --watch src/index.js",
    "test": "node --test"
  },
  "dependencies": {
    "express": "^4.21.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.0"
  }
}

npm run dev starts the app and restarts it when files change; npm start runs it normally. express is needed in production, nodemon only while developing.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which command installs a package only for development?

Exercise 2 True / False

You should commit node_modules to Git.

Exercise 3 Fill in the Blank

Run a script named dev with npm ___ dev

Frequently Asked Questions

npm install can update package-lock.json. npm ci deletes node_modules and installs exactly what package-lock.json says, which is faster and more reliable on servers and in CI pipelines.