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)

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.

相关推荐
恋猫de小郭13 分钟前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
崔庆才丨静觅7 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby60617 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了8 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅8 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅8 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅8 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment8 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅9 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊9 小时前
jwt介绍
前端