NPM常用命令解读
# NPM文档
npm -h
打开帮助文档
npm install install all the dependencies in your project
npm install <foo> add the <foo> dependency to your project
npm test run this project's tests
npm run <foo> run the script named <foo>
npm <command> -h quick help on <command>
npm -l display usage info for all commands -- 简单说明命令
npm help npm more involved overview (in a browser)
1
2
3
4
5
6
7
2
3
4
5
6
7
工作中最常用的
npm install / npm i
,这无疑是最常用的npm run xxx / bun xxx
,调用在 package.json 里定义的各种脚本npm install XX-pack
:安装依赖并且在 package.json 文件内注册- 正确区分和管理
dependencies
与devDependencies
。dependencies 是用来放软件的运行时环境;dev~是用来放测试或者开发检查工具的(eg:ESLint) - 软件运行包不能够在安装依赖的同时附加参数--save-dev
- 正确区分和管理
不常用的
npm test
:由 package.json 决定,前端单元测试会用到,一般是对 ts、或者框架进行测试npm init
:开启新项目时用于初始化 package.json文件?
无用命令,为什么?在现代前端开发中极少直接使用
npm init
来从零开始一个新项目。像 Vue CLI (vue create
)、Create React App (npx create-react-app
)、Vite (npm create vite
) 等脚手架工具已经成为创建项目的标准方式。它们为我们做了大量初始化的工作)
# 常用的就这些:
npm adduser Add a registry user account
npm init Create a package.json file
npm install Install a package
npm ls List installed packages
npm outdated Check for outdated packages
npm ping Ping npm registry
npm repo Open package repository page in the browser,例如把github仓库打开了
npm uninstall Remove a package
npm update Update packages
npm version Bump a package version
npm view View registry info //注意aliases: info, show, v
npm whoami Display npm username
npm prune Remove extraneous packages,删除无关的包
npm config list [--json]
npm config set <key>=<value> [<key>=<value> ...]
npm config get [<key> [<key> ...]]
npm docs dayjs // 直接跳转到dayjs的官网查看文档
npm search jquery // 搜索包,会列出所有包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 运行 prune 命令,npm CLI 会读取
package.json
并将结果与项目的/node_modules
目录进行对比,并打印出不在 package.json 之列的模块列表。 prune:修剪 - npm install
- 安装代码检查等依赖组件:
-D
==--save-dev
- 安装代码检查等依赖组件:
- 幽灵依赖 (Phantom Dependencies):
- Define:指那些你的项目代码直接
require()
或import
使用,并且确实存在于node_modules
文件夹中,但没有在package.json
的dependencies
或devDependencies
中明确声明的包。 - Ah!对于像我这样在2020年之后才进入这个领域的开发者来说,普遍意义上的‘幽灵依赖’问题更多地像是一个需要了解的‘历史遗留问题’。但不可否认的是,自从 npm5.x 版本发布之后,确实解决了幽灵依赖这个用户痛点。
- 当然,也有可能真的存在一些老旧的项目。我们可以用例如
depcheck
工具去检查幽灵依赖(对于 vue 应用可能需要一些手动检查的方式保证不出错)
- Define:指那些你的项目代码直接
完善页面 (opens new window)