Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm 常用知识点 #22

Open
huangshuwei opened this issue Feb 14, 2019 · 0 comments
Open

npm 常用知识点 #22

huangshuwei opened this issue Feb 14, 2019 · 0 comments

Comments

@huangshuwei
Copy link
Owner

huangshuwei commented Feb 14, 2019

安装、更新 npm

使用npm 首先你要先安装nodejs,如果要安装nodejs 请移步 nodejs官网
安装和更新npm 都是使用如下命令:

$ npm install npm -g

更改 npm 的缓存、全局包文件夹

npm 安装完成后,除了会在nodejs 安装目录中多出一个 npm 目录外,还有两个文件夹比较特别,这两个文件夹不在nodejs 的安装目录下(我的是在C:/Users/Administrator/AppData/Roaming/下)。这两个文件夹分别是:

  • npm-cache:缓存模块安装目录
  • npm:全局模块安装目录

随着通过npm 安装的包的增加 npm-cache、npm 中的文件将不断地增大(我本地已经几百兆),有必要将这两个文件夹转移到非C盘的磁盘中。

改变npm-cache 的目录

获取 npm-cache 目录路径

$ npm config get cache

通过命令修改npm-cache 的目录地址(以D盘为例,按需求自行修改)

$ npm config set cache "D:\Program Files\npm-cache"

改变npm 的目录

我们可以通过命令获取 npm 目录的路径

$ npm config get prefix

修改 npm 的目录地址

$ npm config set prefix "D:\Program Files\npm-global"

修改完成后,我们是无法正常使用已经安装的全局包的。
需要修改‘PATH’环境变量:“电脑>属性>高级>环境变量”,环境变量需要增加npm 目录的地址。如:D:/Program Files/npm-global
如果要使修改立即生效,你需要重启电脑,如果你不想重启你可参考这里,或者自行搜索。

更新本地包

检查本地有哪些本地包,首先你要cd 到你想要检查的目录下,然后

$ npm outdated

那么会列出需要更新的包的信息,需要更新的包的名称、当前版本号、最新的版本号等。
1

更新全部

$ npm update

然后通过命令验证是否更新

$ npm outdated

部分更新

如果你只是想更新某一个包到指定的版本,那么你可以直接通过命令修改。如果这个包在package.json 的dependencies 节点下,使用:

$ npm install grunt@1.0.1 --save

如果在package.json 的devDependencies下使用:

$ npm install grunt@1.0.1 --save-dev

然后通过命令验证是否更新

$ npm outdated

更新全局包

检查全局包是否有更新

$ npm outdated -g --depth=0

如果有会呈现如下的信息(以我的为例)

2

全部更新

$ npm update -g

部分更新

npm install -g webpack

查看当前依赖包存在的漏洞

$ npm audit

查看某个安装包的版本记录

$ npm view react versions

切换镜像(由于国内网络太烂,你一定需要)

推荐使用 nrm

$ npm i -g nrm

查看有哪些镜像可以切换
通常有这些:
npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
taobao ----- https://registry.npm.taobao.org/
nj --------- https://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/

$ nrm ls

切换到你需要使用的镜像

切换后的结果一般存储在 C:\Users\Administrator.npmrc 文件中

$ nrm use taobao

npm 包发布

npm publish

此时要注意的是,如果你用的是镜像,那么你需要切回到 npm才可以发布

$ nrm use npm

但是问题来了,你切回来之后可能就无法正常发布,因为网络可能会不通。还好npm 提供了代理功能:

如果你此时没有代理服务,那么你可能需要梯子了

$ npm config set proxy=你的代理服务

查看当前设置的代理

$ npm config get proxy

npm get [<key>]也可查看其他配置

删除代理
如果你发现你的代理设置错了,或者不想用代理了。可以删除掉

$ npm config delete proxy

未完待续(会继续补充)......

@huangshuwei huangshuwei changed the title npm 知识点 npm 常用知识点 Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant