npm quick start for beginner

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

CommandDescription
npm initInitialize a project
npm install <pkg>Install a dependency
npm install -g <pkg>Install globally
npm uninstall <pkg>Remove a dependency
npm updateUpdate 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值