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.

相关推荐
fouryears_234171 小时前
现代 Android 后台应用读取剪贴板最佳实践
android·前端·flutter·dart
boolean的主人1 小时前
mac电脑安装nvm
前端
用户1972959188911 小时前
WKWebView的重定向(objective_c)
前端·ios
烟袅1 小时前
5 分钟把 Coze 智能体嵌入网页:原生 JS + Vite 极简方案
前端·javascript·llm
18你磊哥2 小时前
Django WEB 简单项目创建与结构讲解
前端·python·django·sqlite
KangJX2 小时前
iOS 语音房(拍卖房)开发实践
前端·前端框架·客户端
神秘的猪头2 小时前
🧠 深入理解 JavaScript Promise 与 `Promise.all`:从原型链到异步编程实战
前端·javascript·面试
白兰地空瓶2 小时前
从「似懂非懂」到「了如指掌」:Promise 与原型链全维度拆解
前端·javascript
麦麦在写代码2 小时前
前端学习5
前端·学习
YF02112 小时前
Frida for MacBook/Android 安装配置
android·前端