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)
sudo yum install -y nodejs npm
Using package manager (Debian/Ubuntu)
sudo apt update
sudo apt install -y nodejs npm
Verify installation
node -v
npm -v
Tip: For latest versions, use NodeSource repository:
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs
Or for Debian/Ubuntu:
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:
mkdir my-app && cd my-app
npm init -y
This creates package.json with default settings.
✅ 3. Install Dependencies
Install a runtime dependency:
npm install lodash
Install a development dependency:
npm install --save-dev eslint
✅ 4. Use Installed Packages
Create index.js:
const _ = require('lodash');
console.log(_.shuffle([1, 2, 3, 4]));
Run the script:
node index.js
✅ 5. Manage Dependencies
Update all dependencies:
npm update
Remove a dependency:
npm uninstall lodash
✅ 6. Run Scripts
Add a script in package.json:
"scripts": {
"start": "node index.js"
}
Run it:
npm start
✅ 7. Publish Your Package (Optional)
Login to npm:
npm login
Publish:
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.


209

被折叠的 条评论
为什么被折叠?



