# npm
使用npm安装模块分为本地安装和全局安装。
本地安装:npm install express 会安装到当前项目
全局安装:npm install express -g 会安装到指定的目录(node_global)
另外,如果没有设置全局目录node_global,那么全局安装的文件将会保存到 C:\Users\hades\AppData\Roaming\npm (hases是自己设置的计算机名字)
安装好node后,要设置一下node_global和node_cache(node缓存文件夹)
在node安装目录创建node_global和node_cache文件夹
设置环境变量:
用户变量设置:将用户变量中 PATH 的值改成 D:\Program Files\nodejs\node_global,没有PATH,可以直接添加。
系统变量设置:添加变量 NODE_PATH 值为:D:\Program Files\nodejs\node_modules
- 打开cmd,执行
npm config set prefix "D:\Program Files\nodejs\node_global"
npm config set cache "D:\Program Files\nodejs\node_cache"
1
2
3
2
3
- 有时候用npm拉取包可能会很慢,可以用淘宝npm镜像代替npm进行拉包,就像github和gitee
执行:
npm install -g cnpm --registry=https://registry.npm.taobao.org
1
# yarn 2.0 以上
# 先安装1.X
npm -g install yarn
# 安装3.x版本
export https_proxy=http://192.168.2.10:7890
yarn set version stable
# 查看版本号
yarn --version
# 本机设置(全局设置)
vim ~/.yarnrc.yml
globalFolder: "D:\\repositories\\yarn3-repository\\Data\\global"
yarnPath: "D:\\repositories\\yarn3-repository\\.yarn\\releases\\yarn-3.2.0.cjs"
cacheFolder: "D:\\repositories\\yarn3-repository\\Cache"
installStatePath: "D:\\repositories\\yarn3-repository\\.yarn\\install-state.gz"
npmRegistryServer: "https://repo.huaweicloud.com/repository/npm/"
# angular 项目必须设置 默认pnp
nodeLinker: node-modules
# 单个项目设置
yarn config set prefix "D:\repositories\yarn3-repository\Data"
yarn config set globalFolder "D:\repositories\yarn3-repository\Data\global"
yarn config set yarnPath "D:\repositories\yarn-repository\.yarn\releases\yarn-3.2.0-rc.10.cjs"
yarn config set installStatePath "D:\repositories\yarn3-repository\.yarn\install-state.gz"
yarn config set cacheFolder "D:\repositories\yarn3-repository\Cache"
yarn config set link-folder "D:\repositories\yarn3-repository\Data\link"
yarn config set pnpUnpluggedFolder "D:\repositories\yarn3-repository\.yarn\unplugged"
yarn config set npmRegistryServer "http://127.0.0.1:8081/repository/npm_huawei"
yarn config set unsafeHttpWhitelist --json '["127.0.0.1"]'
# nodeLinker 默认值是 pnp, yarn install后不生成node_modules目录,项目运行有些依赖会报错,建议直接改 node-modules
yarn config set nodeLinker node_modules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Accessing the list of commands
yarn help
1
# Starting a new project
yarn init
1
# Installing all the dependencies
yarn
yarn install
1
2
2
# Adding a dependency
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
1
2
3
2
3
# Adding a dependency to different categories of dependencies
yarn add [package] --dev # dev dependencies
yarn add [package] --peer # peer dependencies
1
2
2
# Adding a global dependency(not support)
Adding a global dependency (opens new window)
yarn global add [package]
1
# Upgrading a dependency
yarn up [package]
yarn up [package]@[version]
yarn up [package]@[tag]
1
2
3
2
3
# Removing a dependency
yarn remove [package]
1
# Upgrading Yarn itself
yarn set version latest
yarn set version from sources
1
2
2