npm Quick Start Guide for Linux
✅ 1. Install Node.js and npm on Linux
npm comes bundled with Node.js. To install both:
Using package manager (RHEL/CentOS/Fedora)
bash
sudo yum install -y nodejs npm
Using package manager (Debian/Ubuntu)
bash
sudo apt update
sudo apt install -y nodejs npm
Verify installation
bash
node -v
npm -v
Tip: For latest versions, use NodeSource repository:
bash
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs
Or for Debian/Ubuntu:
bash
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
✅ 2. Initialize a Project
Create a new directory and initialize npm:
bash
mkdir my-app && cd my-app
npm init -y
This creates package.json with default settings.
✅ 3. Install Dependencies
Install a runtime dependency:
bash
npm install lodash
Install a development dependency:
bash
npm install --save-dev eslint
✅ 4. Use Installed Packages
Create index.js:
javascript
const _ = require('lodash');
console.log(_.shuffle([1, 2, 3, 4]));
Run the script:
bash
node index.js
✅ 5. Manage Dependencies
Update all dependencies:
bash
npm update
Remove a dependency:
bash
npm uninstall lodash
✅ 6. Run Scripts
Add a script in package.json:
json
"scripts": {
"start": "node index.js"
}
Run it:
bash
npm start
✅ 7. Publish Your Package (Optional)
Login to npm:
bash
npm login
Publish:
bash
npm publish
✅ Common Commands Summary
| Command | Description |
|---|---|
npm init |
Initialize a project |
npm install <pkg> |
Install a dependency |
npm install -g <pkg> |
Install globally |
npm uninstall <pkg> |
Remove a dependency |
npm update |
Update dependencies |
npm run <script> |
Run a script |
✅ Summary
npm is the default package manager for Node.js, enabling easy dependency management, automation via scripts, and access to the largest JavaScript package ecosystem.