Common Commands
Command:
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.
✓ Creates package.json with project metadata
✓ Sets up basic project structure
✓ Prepares project for dependency management
Install a specific package:
Installs the express package and adds it to dependencies
Install all dependencies:
Installs all packages listed in package.json
Install as dev dependency:
Installs packages only needed during development
Install globally:
Installs package globally on your system
express - Web framework
mongoose - MongoDB ODM
axios - HTTP client
dotenv - Environment variables
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:
Updates all dependencies to their latest compatible versions
Update specific package:
Updates only the express package
Check for outdated packages:
Lists packages that have newer versions available
Lists all installed packages in a tree structure
Lists only top-level packages
Searches for packages in npm registry
Shows package information
Checks for security vulnerabilities
Automatically fixes security issues
Understanding package.json
dependencies
Packages required for the application to run in production. These are essential for your app to function.
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.
nodemon - Auto-restart server
jest - Testing framework
eslint - Code linting
prettier - Code formatting
webpack - Module bundler
Scripts allow you to define custom commands in package.json to streamline your workflow.
Run with: npm start
Example: "start": "node app.js"
Starts your application in production mode
Run with: npm test
Example: "test": "jest"
Executes your test suite
Run with: npm run lint
Example: "lint": "eslint ."
Checks code quality and style
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
The package name (required, lowercase, no spaces)
Package version following semantic versioning (e.g., 1.0.0)
Brief description of your project
Entry point file (e.g., "index.js" or "app.js")
Custom commands for your project
Array of keywords for npm search
Package author's name
License type (e.g., "MIT", "ISC")
GitHub repository URL
Specifies Node.js and npm versions required
{
"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"
}
}
npm uses semantic versioning (semver) in the format: MAJOR.MINOR.PATCH
^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