SyntaxStudy
Sign Up
Node.js npm Basics: init, install, and package.json
Node.js Beginner 10 min read

npm Basics: init, install, and package.json

npm (Node Package Manager) is the default package manager for Node.js. It hosts over 2 million packages and manages project dependencies through package.json.

Every Node project starts with npm init which creates the package.json manifest file.

Example
# Initialize a new project (interactive):
npm init

# Initialize with defaults (skips questions):
npm init -y

# Install a package and save to dependencies:
npm install express

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

# Install a specific version:
npm install lodash@4.17.21

# Install all dependencies from package.json:
npm install

# Uninstall a package:
npm uninstall express

# List installed packages:
npm list --depth=0

# package.json structure:
# {
#   "name": "my-app",
#   "version": "1.0.0",
#   "main": "index.js",
#   "scripts": {
#     "start": "node index.js",
#     "dev": "nodemon index.js",
#     "test": "jest"
#   },
#   "dependencies": { "express": "^4.18.2" },
#   "devDependencies": { "nodemon": "^3.0.1" }
# }