📦 Working with npm & Package.json

Complete guide to Node Package Manager and package configuration

Common Commands

npmInitialize Project â–¼

Command:

npm init -y

Initialize a new package.json file with default values

This command creates a package.json file in your project directory. The -y flag accepts all default options automatically.

What it does:

✓ Creates package.json with project metadata

✓ Sets up basic project structure

✓ Prepares project for dependency management

npmInstall Packages â–¼

Install a specific package:

npm install express

Installs the express package and adds it to dependencies

Install all dependencies:

npm install

Installs all packages listed in package.json

Install as dev dependency:

npm install --save-dev jest
npm install -D nodemon

Installs packages only needed during development

Install globally:

npm install -g nodemon

Installs package globally on your system

Common packages to install:

express - Web framework

mongoose - MongoDB ODM

axios - HTTP client

dotenv - Environment variables

npmUninstall Packages â–¼
npm uninstall express

Removes the express package from your project

This command removes the package from node_modules folder and removes the entry from package.json dependencies.

Alternative commands:

npm remove express
npm rm express
npm un express
npmUpdate Dependencies â–¼
npm update

Updates all dependencies to their latest compatible versions

Update specific package:

npm update express

Updates only the express package

Check for outdated packages:

npm outdated

Lists packages that have newer versions available

npmOther Useful Commands â–¼
npm list

Lists all installed packages in a tree structure

npm list --depth=0

Lists only top-level packages

npm search express

Searches for packages in npm registry

npm view express

Shows package information

npm audit

Checks for security vulnerabilities

npm audit fix

Automatically fixes security issues

Understanding package.json

dependencies vs devDependencies â–¼

dependencies

Packages required for the application to run in production. These are essential for your app to function.

Examples of dependencies:

express - Web server framework

mongoose - Database ORM

axios - HTTP requests

dotenv - Environment variables

bcrypt - Password hashing

devDependencies

Packages only needed during development and testing. Not required in production.

Examples of devDependencies:

nodemon - Auto-restart server

jest - Testing framework

eslint - Code linting

prettier - Code formatting

webpack - Module bundler

Scripts for running commands â–¼

Scripts allow you to define custom commands in package.json to streamline your workflow.

Common script examples:
start

Run with: npm start

Example: "start": "node app.js"

Starts your application in production mode

test

Run with: npm test

Example: "test": "jest"

Executes your test suite

lint

Run with: npm run lint

Example: "lint": "eslint ."

Checks code quality and style

dev

Run with: npm run dev

Example: "dev": "nodemon app.js"

Starts with auto-reload for development

Note: start and test are special scripts that don't need "run" keyword. All other scripts require npm run script-name

Important package.json Fields â–¼
"name"

The package name (required, lowercase, no spaces)

"version"

Package version following semantic versioning (e.g., 1.0.0)

"description"

Brief description of your project

"main"

Entry point file (e.g., "index.js" or "app.js")

"scripts"

Custom commands for your project

"keywords"

Array of keywords for npm search

"author"

Package author's name

"license"

License type (e.g., "MIT", "ISC")

"repository"

GitHub repository URL

"engines"

Specifies Node.js and npm versions required

Example package.json structure:
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "My awesome Node.js app",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.20"
  }
}
Version Numbers Explained â–¼

npm uses semantic versioning (semver) in the format: MAJOR.MINOR.PATCH

Version symbols:

^4.18.2 (Caret)

Allows updates that don't change the leftmost non-zero digit (e.g., 4.18.2 → 4.19.0 ✓, but not 5.0.0 ✗)

~4.18.2 (Tilde)

Allows only patch updates (e.g., 4.18.2 → 4.18.5 ✓, but not 4.19.0 ✗)

4.18.2 (Exact)

Installs exactly this version only

* or latest

Installs the latest version available