1. 安装Node.js
https://nodejs.org/zh-cn/download/
npm 是 Node.js 的默认包管理器,安装好 Node.js 后 npm 也安装好了。
可以使用nvm (macOS/Linux)或nvm-windows在不同的项目之间切换Node版本。
2、npm 相关设置
2.1 安装nrm,nrm能够管理npm源:
npm install -g nrm --registry https://registry.npmmirror.com/
查看当前可用源
nrm ls
2.2 npm更换源
# npm更换源
npm config get registry
npm config set registry https://registry.npm.taobao.org/
或者
npm config set registry https://registry.npmmirror.com/
# npm 恢复官方源
npm config set registry https://registry.npmjs.org
3、npm 常用命令
npm -h
查看帮助
npm <command>
3.1 npm install
npm install
install all the dependencies in your project
npm install <foo>
add the dependency to your project
全局安装与本地安装:
npm install express # 本地安装,当前⽂件夹下⽣成node_modules⽂件夹
npm install express -g # 全局安装,在Nodejs⽬录下创建
npm install [<@scope>/]<name>:
@scope
所有的npm包都有一个名字。有些包名也有作用域。作用域遵循包名的通常规则(url安全的字符,没有前导的点或下划线)。当在包名中使用时,作用域的前面是@符号,后面是斜杠。
@somescope/somepackagename
作用域是一种将相关包分组在一起的方法,它也会影响npm处理包的方式。
每个npm用户/组织都有自己的作用域,只有你可以在你的作用域中添加包。这意味着您不必担心有人在您之前使用您的包名。因此,它也是向组织发出官方软件包信号的好方法
作用域包可以从npm@2发布和安装,并得到主npm注册中心的支持
–save-…
NPM install默认将任何指定的包保存到dependencies
中。此外,你可以用一些附加的标志来控制它们的保存位置和方式:-P, --save-prod
: Package will appear in your dependencies. This is the default unless -D or -O are present.
-D, --save-dev
: Package will appear in your devDependencies.
-O, --save-optional
: Package will appear in your optionalDependencies.
--no-save
: Prevents saving to dependencies.
当使用上述任何选项将依赖项保存到package.json
中时,有两个额外的可选标志:-E,——save-exact
:保存的依赖项将被配置为精确的版本,而不是使用npm默认的semver range操作符。-B,——save-bundle
:保存的依赖也会被添加到你的bundleDependencies列表中。
此外,如果你有一个npm-shrinkwrap.json 或package-lock.json,那么它也将被更新
npm uninstall
npm uninstall express
卸载模块
npm update express
npm update express
更新模块
3.2 npm test
run this project’s tests
3.3 npm run <foo>
run the script named
3.4 npm -l
display usage info for all commands
3.5 npm help
npm help <term>
search for help on <term> (in a browser)
npm help npm
more involved overview (in a browser)
npm help config
获取更多的配置信息
npm help 7 config
更多关于npm的配置字段
3.6 npm config
管理npm的配置文件
3.6.1 list
npm config list
显示所有配置设置。使用-l
也可以显示默认值。使用--json
以json格式显示设置。
3.6.2 set
npm config set key=value [key=value...]
npm set key=value [key=value...]
将每个配置键设置为提供的值。
如果省略value,则将其设置为空字符串。
3.6.3 get
npm config get [key ...]
npm get [key ...]
将配置值回显到stdout。
如果提供了多个键,则值将以键名作为前缀。
如果没有提供密钥,则此命令的行为与npm config list相同。
3.64 delete
npm config delete key [key ...]
从所有配置文件中删除指定的键。