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.

相关推荐
万少2 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站4 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名7 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫7 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊7 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter7 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折8 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_8 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial8 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu8 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端