CPCODELAB
Node.js

npm & package.json

10 min read

npm & package.json

npm (Node Package Manager) is installed alongside Node.js and is the world's largest software registry. It manages your project's dependencies, scripts, and metadata through a package.json file at the root of every Node.js project.

bash
# Create a new project with package.json
mkdir my-app && cd my-app
npm init -y
json
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "My first Node.js app",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "node --test"
  },
  "dependencies": {},
  "devDependencies": {}
}

Installing Packages

bash
# Install a runtime dependency
npm install express

# Install a dev-only dependency
npm install --save-dev nodemon

# Install globally
npm install -g typescript

# Remove a package
npm uninstall express

Always commit package.json and package-lock.json to version control. The node_modules/ folder should be in .gitignore.

Ready to test yourself?
Practice Node.js— quiz & coding exercises