npm tutorial

By Xah Lee. Date: .
xtodo

What is npm?

npm is software repository for JavaScript code.

npm is a command-line tool to manage JavaScript/TypeScript packages.

npm is bundled with Node.js.

2. install node.js

download at https://nodejs.org

Verify installation:

node -v

npm -v
npm version 2026-07-06 316d8 ll
npm version 2026-07-06 316d8 ll

Create project

mkdir my-t-app
cd my-t-app

# Initialize a new project (creates package.json)
npm init -y

-y means don't ask questions

package.json is the heart of every npm project. Example:

{
  "name": "my-t-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

installing packages

Install dependency

npm install express
# or short form
npm i express

Local packages are installed in node_modules dir of current dir.

Save as devDependency (only for development)

npm install --save-dev nodemon typescript
# or
npm i -D nodemon typescript

Install a specific version

npm i lodash@4.17.21

Install globally (CLI tools)

npm install -g create-vite

npm global module location

# show npm global install dir
npm root -g

usually

5. package.json fields you should know

Field Purpose Example
name Package name "my-cool-lib"
version Semantic versioning "1.2.3"
scripts Custom commands "dev": "vite"
dependencies Production packages { "express": "^4.19.2" }
devDependencies Dev-only packages { "vite": "^5.4.0" }
type Use ES modules ("module") "module"

6. npm scripts (most useful feature)

Edit the "scripts" section in package.json:

{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "build": "tsc",
    "test": "jest",
    "lint": "eslint .",
    "format": "prettier --write \"**/*.{js,ts}\""
  }
}

Run them with:

npm start
npm run dev        # use "run" for custom scripts
npm test

7. common commands cheat sheet

# Install all dependencies in a project
npm install
# or
npm i

# Update packages
npm update

# Check for outdated packages
npm outdated

# Uninstall a package
npm uninstall express
npm rm express

# Clean install (fixes weird issues)
rm -rf node_modules package-lock.json
npm install

# View global packages
npm list -g --depth=0

8. package-lock.json vs yarn.lock / pnpm-lock.yaml


9. publishing your own package

  1. Create an account: https://www.npmjs.com
  2. In your project:
npm login
npm publish

Pro tips for publishing:


10. modern best practices (2026)

corepack enable

Bonus: quick start with vite (recommended)

npm create vite@latest my-react-app -- --template react-ts
cd my-react-app
npm install
npm run dev

You now know enough npm to build real applications!

Want a follow-up tutorial on any of these?

npm