Windows安装及配置NPM、YARN、PNPM

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
39
40
41
1:首先下载node.js
 
https://nodejs.org/zh-cn
 
2:查看是否安装成功
 
node -v    安装成功后会有个版本号
 
3:安装npm镜像   <br>
npm config set registry https://registry.npmmirror.com
 
4:配置npm全局模块存放路径及cache路径
 
npm config set prefix "C:\Users\xxxxx\.nodejs\node_global"
npm config set cache "C:\Users\xxxxx\.nodejs\node_cache"
 
5:配置信息是否成功
 
显示配置信息
npm config list
 
检查镜像
npm config get registry
 
6:常用命令
 
本地安装(local)
 
npm install gulp
 
全局安装(global),使用 -g 或 --global
 
npm install gulp -g
 
npm uninstall 卸载模块
 
npm update 更新模块
 
npm ls 查看安装的模块
 
npm init 在项目中引导创建一个package.json文件​​

 

复制代码
方式1:

#启用 Corepack(Node.js 默认附带的工具)即可
#这会将 yarn 二进制文件添加到您的 PATH 中:
corepack enable

#https://yarnpkg.com/getting-started/install

方式2:

1.安装yarn

npm install -g yarn

2. 安装成功后,查看版本号:

yarn --version

3:安装yarn镜像

yarn config set registry https://registry.npmmirror.com

查看当前使用的镜像源:

yarn config get registry

4:配置yarn全局模块存放路径及cache路径

yarn config set global-folder "C:\Users\xxxxx\.nodejs\node_global"
yarn config set cache-folder "C:\Users\xxxxx\.nodejs\node_cache"

查看当前的全局安装路径和缓存路径:
yarn global dir
yarn cache dir

5. 初始化项目

yarn init // 同npm init,执行输入信息后,会生成package.json文件

6. yarn的配置项:

yarn config list // 显示所有配置项

yarn config get <key> //显示某配置项

yarn config delete <key> //删除某配置项

yarn config set <key> <value> [-g|--global] //设置配置项

7.安装包:

yarn install //安装package.json里所有包,并将包及它的所有依赖项保存进yarn.lock
yarn install --flat //安装一个包的单一版本
yarn install --force //强制重新下载所有包
yarn install --production //只安装dependencies里的包
yarn install --no-lockfile //不读取或生成yarn.lock
yarn install --pure-lockfile //不生成yarn.lock

8. 添加包(会更新package.json和yarn.lock):

yarn add [package] // 在当前的项目中添加一个依赖包,会自动更新到package.json和yarn.lock文件中
yarn add[package]@[version] // 安装指定版本,这里指的是主要版本,如果需要精确到小版本,使用-E参数
yarn add[package]@[tag] // 安装某个tag(比如beta,next或者latest)//

//不指定依赖类型默认安装到dependencies里,你也可以指定依赖类型:

yarn add --dev/-D // 加到 devDependencies
yarn add --peer/-P // 加到 peerDependencies
yarn add --optional/-O // 加到 optionalDependencies

//默认安装包的主要版本里的最新版本,下面两个命令可以指定版本:

yarn add --exact/-E // 安装包的精确版本。例如yarn add test@2.2.0会接受2.7.0版,但yarn add test@2.2.0 --exact只会接受2.2.0版
yarn add --tilde/-T // 安装包的次要版本里的最新版。例如yarn add test@2.2.1 --tilde会接受2.2.5,但不接受1.3.0

9. 发布包

yarn publish

10. 移除一个包

yarn remove <packageName>:移除一个包,会自动更新package.json和yarn.lock

11. 更新一个依赖

yarn upgrade 用于更新包到基于规范范围的最新版本

12. 运行脚本

yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本

13. 显示某个包的信息

yarn info <packageName> 可以用来查看某个模块的最新版本信息

14. 缓存

yarn cache

yarn cache list # 列出已缓存的每个包
yarn cache dir # 返回 全局缓存位置
yarn cache clean # 清除缓存
复制代码

 

复制代码
#安装pnpm

#文档地址:https://pnpm.io/installation

方式1:

#启用 Corepack(Node.js 默认附带的工具)即可安装 pnpm

corepack enable pnpm

方式2:
npm install -g pnpm

#注意:如果遇到下面的错:
错误1:
pnpm config set registry https://registry.npmmirror.com
pnpm : 无法加载文件 C:\Program Files\nodejs\pnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.micros
oft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。

#解决办法:
Set-ExecutionPolicy -Scope CurrentUser  RemoteSigned

错误2:
pnpm config set registry https://registry.npmmirror.com
! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-9.12.3.tgz
? Do you want to continue? [Y/n] y

C:\Program Files\nodejs\node_modules\corepack\dist\lib\corepack.cjs:21609
    throw new Error(
          ^
 see https://github.com/nodejs/corepack#troubleshooting
    at fetch (C:\Program Files\nodejs\node_modules\corepack\dist\lib\corepack.cjs:21609:11)

#我的解决办法是手动打开corepack.cjs,搜索pnmp节点,然后将pnmp节点中的url改为域名改为https://registry.npmmirror.com
#再次执行命令就可以正常下载pnpm包了。

#安装npm镜像   

pnpm config set registry https://registry.npmmirror.com

#配置npm全局模块存放路径及cache路径

pnpm config set cache-dir "C:\Users\anech\.nodejs\node_cache"
pnpm config set store-dir "C:\Users\anech\.nodejs\node_global"

#配置信息是否成功

#显示配置信息
pnpm config list 

#检查镜像
pnpm config get registry

#您可以使用以下命令固定项目上使用的 pnpm 版本:
corepack use pnpm@latest
pnpm install 包
pnpm i 包
pnpm add 包 -S 默认写入dependencies
pnpm add -D -D devDependencies
pnpm add -g 全局安装
复制代码

 

1
2
3
4
5
自定义安装仅需选择:
 
Node.js runtime
npm package manager
Add to PATH

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
环境变量配置
 
系统变量下的【Path】中,增加node.exe的目录
 
C:\Program Files\nodejs
 
系统变量下新建【NODE_PATH】,设置第三方依赖包安装目录
 
C:\Program Files\nodejs\node_modules
 
用户变量下的【Path】中,npm的路径(C:\Users\xxxxx\AppData\Roaming\npm
)改为指定全局安装路径
 
C:\Users\xxxxx\.nodejs\node_global

  

posted @   anech  阅读(916)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示