编程随笔 编程随笔
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 嵌入式
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • 知识点

    • npm 和 yarn
      • 一. 清除缓存
      • 二. npm和yarn源的简单修改(以淘宝镜像为例)
      • 三. npm和yarn修改和查看全局安装路径和缓存路径
      • 四. 使用第三方软件快速修改、切换 npm和yarn的源(以淘宝镜像为例)
      • CLI 命令比较
    • 登录后重定向到原先路由
    • patch-package使用
    • TortosseGit的ssh配置
    • nginx常用配置
    • mounted阶段获取不到dom的原因及解决方法
  • 代码调试

  • vue2

  • vue3

  • react

  • typescript

  • 前端
  • 知识点
华总
2023-08-21
0
0
目录

npm 和 yarn原创

提示

作为前端开发者我们经常会使用npm或yarn,我们都知道npm和yarn默认镜像在国外,国内访问这些镜像速度会比较慢,下面介绍修改npm和yarn源的方法

# 一. 清除缓存

# npm

npm cache clean --force
1

# yarn

yarn cache clean
1

# pnpm

pnpm store prune
1

# 二. npm和yarn源的简单修改(以淘宝镜像为例)

# npm

# 1. 临时修改(只生效一次)
npm install 包的名字 --registry https://registry.npmmirror.com
1
# 2. 设置npm的配置项(全局配置)

查看npm源的当前地址

npm config get registry
1

设置淘宝镜像

npm config set registry https://registry.npmmirror.com
1

设置npm官方镜像

npm config set registry https://registry.npmjs.org/
1

# yarn

# 1. 临时修改(只生效一次)
yarn save 包的名字 --registry https://registry.npmmirror.com/
1
# 2. 设置yarn的配置项(全局配置)

查看yarn源的当前地址

yarn config get registry
1

设置淘宝镜像

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

设置yarn官方镜像

yarn config set registry https://registry.yarnpkg.com
1

# 三. npm和yarn修改和查看全局安装路径和缓存路径

# npm的安装和修改

# 查看各种路径命令

  • 查看当前npm包的全局安装路径

    npm prefix -g 
    
    1
  • 查看配置列表

    npm config ls 
    
    1

# 修改路径命令

  • 修改npm的包的全局安装路径

    npm config set prefix "D:\software\nodeJs\info\node_global"
    
    1
  • 修改npm的包的全局cache位置

    npm config set cache "D:/Users/nodejs/npm_cache"
    
    1

# 配置环境变量

将全局路径配置在环境变量中

此电脑 -> 属性 -> 高级系统设置 -> 环境变量 -> 系统变量 -> path ->编辑 - > 新增路径 -D:\software\nodeJs\info\node_global (路径可以根据npm prefix -g查看)

详情参考:yarn的配置 (opens new window)

# yarn 的安装路径和缓存路径

# 查看各种路径命令

  • 查看 yarn 全局bin位置

    yarn global bin
    
    1
  • 查看 yarn 全局安装位置

    yarn global dir
    
    1
  • 查看 yarn 全局cache位置

    yarn cache dir
    
    1

# 修改路径命令

  • 改变 yarn 全局bin位置

    yarn config set prefix "D:\software\Yarn\Data"
    
    1
  • 改变 yarn 全局安装位置

    yarn config  set global-folder "D:\software\Yarn\Data\global"
    
    1
  • 改变 yarn 全局cache位置

    yarn config set cache-folder "D:\software\Yarn\Cache"
    
    1
  • 改变 yarn 全局 link 位置

    yarn config set link-folder "D:\software\Yarn\Data\link"
    
    1

# 四. 使用第三方软件快速修改、切换 npm和yarn的源(以淘宝镜像为例)

# (1)分别修改npm和yarn源(经实际测试,这种方法修改一个源另一个源也会同时修改)

# npm

nrm 是一个 NPM 源管理器,允许你快速地在如下 NPM 源间切换

# 安装
npm install -g nrm
1
# 列出可选的源
nrm ls

* npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
  taobao - https://registry.npmmirror.com/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
1
2
3
4
5
6
7
8
9

带 * 的是当前使用的源

# 切换

切换到taobao镜像源

nrm use taobao

   Registry has been set to: https://registry.npmmirror.com/
1
2
3
# 测试源的响应时间

测试所有源的响应时间:

nrm test

  npm ---- 2930ms
  cnpm --- 300ms
* taobao - 292ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1626ms
  edunpm - Fetch Error
1
2
3
4
5
6
7
8
9

可以多次测量来得到更精确的结果 更多nrm使用方法访问nrm的gitHub仓库 https://github.com/Pana/nrm (opens new window)

# yarn

yrm 是一个 yarn源管理器,允许你快速地在yarn源间切换

# 安装
npm install -g yrm
1
# 列出可选的源
yrm ls

  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
* taobao - https://registry.npmmirror.com/
  nj ----- https://registry.nodejitsu.com/
  rednpm - http://registry.mirror.cqupt.edu.cn/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
  yarn --- https://registry.yarnpkg.com
1
2
3
4
5
6
7
8
9
10

带 * 的是当前使用的源

# 切换

切换到taobao镜像源

yrm use taobao

   YARN Registry has been set to: https://registry.npmmirror.com/
1
2
3
# 测试源的响应时间

测试所有源的响应时间:

yrm test

  npm ---- 784ms
  cnpm --- 290ms
* taobao - 297ms
  nj ----- Fetch Error
  rednpm - Fetch Error
  npmMirror  1353ms
  edunpm - Fetch Error
  yarn --- Fetch Error
1
2
3
4
5
6
7
8
9
10

可以多次测量来得到更精确的结果 更多yrm使用方法访问yrm的gitHub仓库 https://github.com/i5ting/yrm (opens new window)

# (2)使用双源管理工具cgr

# 安装
npm install -g cgr
1

使用方法和前两种差别不大,下边附上使用方法网址https://www.npmjs.com/package/cgr (opens new window)

# CLI 命令比较

npm(v5) 纱
npm install yarn add
不适用 yarn add --flat
*(不适用)* yarn add --har
npm install --no-package-lock yarn add --no-lockfile
*(不适用)* yarn add --pure-lockfile
npm install [package] --save yarn add [package]
npm install [package] --save-dev yarn add [package] --dev
*(不适用)* yarn add [package] --peer
npm install [package] --save-optional yarn add [package] --optional
npm install [package] --save-exact yarn add [package] --exact
*(不适用)* yarn add [package] --tilde
npm install [package] --global yarn global add [package]
npm update --global yarn global upgrade
npm rebuild yarn add --force
npm uninstall [package] yarn remove [package]
npm cache clean yarn cache clean [package]
rm -rf node_modules && npm install yarn upgrade
npm version major yarn version --major
npm version minor yarn version --minor
npm version patch yarn version --patch

【参考文章】

https://www.jianshu.com/p/309645729b2e (opens new window)

https://www.jianshu.com/p/309645729b2e (opens new window)

https://laravel-china.org/articles/15976/yarn-accelerate-and-modify-mirror-source-in-china (opens new window)

https://www.npmjs.com/package/cgr (opens new window)

https://classic.yarnpkg.cn/docs/migrating-from-npm (opens new window)

#前端
上次更新: 2025/02/18 14:46:10
登录后重定向到原先路由

登录后重定向到原先路由→

最近更新
01
jFlash使用 原创
03-24
02
中央仓库上传指南 原创
03-23
03
模板生成工具 原创
02-18
04
RTC实时时钟 原创
02-12
05
keil调试 原创
01-21
更多文章>
Copyright © 2023-2025 liyao52033  All Rights Reserved
备案号:鄂ICP备2023023964号-1    
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式