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.

相关推荐
IT_陈寒26 分钟前
Vite的public文件夹放静态资源?这坑我替你踩了
前端·人工智能·后端
涵涵(互关)39 分钟前
GoView各项目文件中的相关语法2
前端·javascript·vue.js
子兮曰1 小时前
别让爬虫白嫖你的导航站了:纯免费,手把手实现加密字体防爬
前端·javascript·后端
小村儿1 小时前
连载06 - Hooks 源码深度解析:Claude Code 的确定性自动化体系
前端·后端·ai编程
心中无石马1 小时前
uniapp引入tailwindcss4.x
前端·css·uni-app
焰火19992 小时前
[Vue]可重置的响应式状态reactive
前端·vue.js
陆枫Larry2 小时前
CSS transform scale:图片放大效果背后的原理
前端
老王以为2 小时前
为什么 React 和 Vue 不一样?
前端·vue.js·react.js
web打印社区2 小时前
2026最新Web静默打印解决方案,无插件无预览,完美替代Lodop
前端·javascript·vue.js·electron·pdf
这个DBA有点耶2 小时前
分组排名不用窗口函数?那你还在写几十行的子查询
前端·代码规范