npm tutorial
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
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
- Windows:
%AppData%/npm/node_modules(often~/AppData/Roaming/npm/node_modules) - macOS/Linux:
/usr/local/lib/node_modules(or a user-specific path like~/.nvm/versions/node/<version>/lib/node_modulesif using nvm)
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
package-lock.jsonis created automatically by npm- Never delete it if you want reproducible installs
- Commit it to git
9. publishing your own package
- Create an account: https://www.npmjs.com
- In your project:
npm login npm publish
Pro tips for publishing:
- Use scoped packages:
@yourusername/my-lib - Add
.npmignoreor use"files"field in package.json - Use semantic versioning (
major.minor.patch)
10. modern best practices (2026)
- Use npm workspaces for monorepos
- Prefer Vite + TypeScript for new projects
- Use
npm ciin CI/CD (faster, stricter) - Enable Corepack for better package manager control:
corepack enable
- Consider pnpm or Bun for faster installs in large projects (but npm is still excellent)
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 workspaces / monorepos
- Publishing scoped packages
- Security & auditing (
npm audit) - Using
.nvmor Volta - Comparing npm vs pnpm vs Bun