Node.js/yarn:修订间差异
来自康健生活
< Node.js
| (未显示同一用户的24个中间版本) | |||
| 第1行: | 第1行: | ||
== 标题文本 == | |||
[[/特殊配置|特殊配置]] | |||
== 配置文件 == | |||
[[/配置文件|配置文件]] | |||
== 安装 == | == 安装 == | ||
centos | centos | ||
| 第19行: | 第27行: | ||
yarn config set registry <registry-url> | yarn config set registry <registry-url> | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
Yarn 2+ | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set npmRegistryServer <registry-url></syntaxhighlight>|$|/home/kangkk}} | |||
{| class="wikitable" | {| class="wikitable" | ||
| 第34行: | 第47行: | ||
== 配置文件 == | == 配置文件 == | ||
{| class="wikitable" | |||
|- | |||
| <nowiki><2.0</nowiki> || https://classic.yarnpkg.com/en/docs/yarnrc | |||
|- | |||
| <nowiki>>=2.0</nowiki> || https://yarnpkg.com/configuration/manifest | |||
|} | |||
=== .yarnrc === | |||
可以指定包的源 | |||
{{hc|head=.yarnrc(<2.0)|output=<syntaxhighlight lang="bash"> | |||
# .yarnrc | |||
# 设置域的指定源 | |||
"@babel:registry" "https://mirrors.huaweicloud.com/repository/npm/" | |||
"registry" "http://mirrors.cloud.tencent.com/npm/" | |||
</syntaxhighlight>}} | |||
{{hc|head=.yarnrc(>=2.0)|output=<syntaxhighlight lang="bash"> | |||
# .yarnrc.yml | |||
# # 设置域的指定源 | |||
"npmScopes": | |||
"babel": | |||
"npmRegistryServer": "https://mirrors.huaweicloud.com/repository/npm/" | |||
"npmRegistryServer": "http://mirrors.cloud.tencent.com/npm/" | |||
</syntaxhighlight>}} | |||
== 配置 == | |||
=== unsafeHttpWhitelist === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set unsafeHttpWhitelist --json '["*.xxx.com"]'</syntaxhighlight>|$|/home/kangkk}} | |||
=== npmAlwaysAuth === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set npmAlwaysAuth true</syntaxhighlight>|$|/home/kangkk}} | |||
=== npmAuthToken === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set npmAuthToken <token></syntaxhighlight>|$|/home/kangkk}} | |||
=== npmRegistryServer === | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set npmRegistryServer http://npm.xxx.com</syntaxhighlight>|$|/home/kangkk}} | |||
=== nodeLinker === | |||
默认值是 pnp,仍然使用 <code>node-modules</code> 文件夹。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn config set nodeLinker node-modules</syntaxhighlight>|$|/home/kangkk}} | |||
=== 私有镜像源 === | |||
Token 可以在 <code>.npmrc</code> 文件中获取(需要通过 NPM 登录之后)。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn npm login | |||
# or yarn config set npmAuthToken <yourToken></syntaxhighlight>|$|/home/kangkk}} | |||
== 添加 == | == 添加 == | ||
=== 指定包 === | |||
指定版本号 | 指定版本号 | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
| 第52行: | 第118行: | ||
yarn add https://x.y/package.tgz # 指定远程 gzipped 压缩包安装。 | yarn add https://x.y/package.tgz # 指定远程 gzipped 压缩包安装。 | ||
</syntaxhighlight>|$|/home/kangkk}} | </syntaxhighlight>|$|/home/kangkk}} | ||
=== 二进制文件 === | |||
<pre> | |||
# 指定sass的二进制文件下载 | |||
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ | |||
# 指定electron的二进制文件下载 | |||
electron_mirror=https://npm.taobao.org/mirrors/electron/ | |||
类似还有 | |||
--canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas | |||
</pre> | |||
== 代理 == | |||
== 更新依赖 == | == 更新依赖 == | ||
{{shc|<syntaxhighlight lang="bash"> | {{shc|<syntaxhighlight lang="bash"> | ||
yarn upgrade-interactive --latest</syntaxhighlight>|$}} | yarn upgrade-interactive --latest</syntaxhighlight>|$}} | ||
== PNP == | |||
Yarn 2+ | |||
=== .pnp.cjs === | |||
使用.pnp.cjs替代node_modules来帮助node解析包. | |||
== dlx == | |||
== 缓存 == | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn cache clean</syntaxhighlight>|$|/home/kangkk}} | |||
全局缓存 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn cache clean --mirror</syntaxhighlight>|$|/home/kangkk}} | |||
=== 全局缓存 === | |||
=== 硬链接 === | |||
== 编辑器支持 == | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn dlx @yarnpkg/sdks vscode</syntaxhighlight>|$|/home/kangkk}} | |||
== 命令 == | |||
=== policies === | |||
在存储库中签入 Yarn 版本。该命令只会从 GitHub 存储库下载单文件版本,将其存储在项目的 <code>.yarn/releases</code> 文件夹中。 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn policies set-version <version></syntaxhighlight>|$|/home/kangkk}} | |||
<pre> | |||
yarn policies set-version # 下载最新的稳定版本 | |||
yarn policies set-version --rc # 下载最新的 rc 版本 | |||
yarn policies set-version 1.13.0 # 下载特定版本 | |||
yarn policies set-version '^1.12.0' # 下载最新的次要 | |||
yarn policies set-version berry</pre> | |||
升级到最新发布版本 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn set version latest</syntaxhighlight>|$|/home/kangkk}} | |||
== 版本升级 == | |||
从版本 1 升级到版本 4 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
corepack enable \ | |||
&& corepack prepare yarn@4.9.2 --activate</syntaxhighlight>|$|/home/kwy}} | |||
== 插件 == | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn plugin import workspace-tools</syntaxhighlight>|$|/home/kangkk}} | |||
== workspaces == | |||
{{hc|head=package.json|output=<syntaxhighlight lang="bash"> | |||
"workspaces": ["athena-frontend"] | |||
</syntaxhighlight>}} | |||
查看包含的项目及依赖关系 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn workspaces list --json</syntaxhighlight>|$|/home/kangkk}} | |||
在所有 workspaces 包含的所有项目下执行命令 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn workspaces foreach run build</syntaxhighlight>|$|/home/kangkk}} | |||
在所有 workspaces 包含的一个项目下执行命令 | |||
{{shc|<syntaxhighlight lang="bash"> | |||
yarn workspace <workspaceName> <commandName></syntaxhighlight>|$|/home/kangkk}} | |||
=== 子项目 === | |||
{{hc|head=package.json|output=<syntaxhighlight lang="bash"> | |||
... | |||
"packageManager": "yarn@4.4.1", | |||
"workspaces": [ | |||
"athena-frontend", | |||
"src/next/codemirror/language/test/lezer-test" | |||
] | |||
} | |||
</syntaxhighlight>}} | |||
workspaces 中可以是子项目 package.json 的名称或者是相对路径,依据经验而言,对于复杂项目(子项目位置不统一)的场景,相对路径是更好的选择。 | |||
2025年8月2日 (六) 15:37的最新版本
标题文本
配置文件
安装
centos
npm install -g yarn
mac
curl -o- -L https://yarnpkg.com/install.sh | bash
源
配置源
查询当前使用的镜像源
/home/kangkk
yarn config get registry
设置源
/home/kangkk
yarn config set registry <registry-url>
Yarn 2+
/home/kangkk
yarn config set npmRegistryServer <registry-url>
| 淘宝镜像源 | https://registry.npmmirror.com/ |
| 腾讯云镜像源 | https://mirrors.cloud.tencent.com/npm/ |
| 官方镜像源 | https://registry.yarnpkg.com/ |
源工具
/home/kangkk
npm install -g yrm
配置文件
| <2.0 | https://classic.yarnpkg.com/en/docs/yarnrc |
| >=2.0 | https://yarnpkg.com/configuration/manifest |
.yarnrc
可以指定包的源
.yarnrc(<2.0)
# .yarnrc
# 设置域的指定源
"@babel:registry" "https://mirrors.huaweicloud.com/repository/npm/"
"registry" "http://mirrors.cloud.tencent.com/npm/"
.yarnrc(>=2.0)
# .yarnrc.yml
# # 设置域的指定源
"npmScopes":
"babel":
"npmRegistryServer": "https://mirrors.huaweicloud.com/repository/npm/"
"npmRegistryServer": "http://mirrors.cloud.tencent.com/npm/"
配置
unsafeHttpWhitelist
/home/kangkk
yarn config set unsafeHttpWhitelist --json '["*.xxx.com"]'
npmAlwaysAuth
/home/kangkk
yarn config set npmAlwaysAuth true
npmAuthToken
/home/kangkk
yarn config set npmAuthToken <token>
npmRegistryServer
/home/kangkk
yarn config set npmRegistryServer http://npm.xxx.com
nodeLinker
默认值是 pnp,仍然使用 node-modules 文件夹。
/home/kangkk
yarn config set nodeLinker node-modules
私有镜像源
Token 可以在 .npmrc 文件中获取(需要通过 NPM 登录之后)。
/home/kangkk
yarn npm login
# or yarn config set npmAuthToken <yourToken>
添加
指定包
指定版本号
/home/kangkk
yarn add package-name # 安装最新版本
yarn add package-name@0.0.1 # 安装指定版本
yarn add package-name@tag # 安装指定标识版本
指定路径
/home/kangkk
yarn add package-name # 指定配置源安装,也可 package.json 指定源安装
yarn add file:/path/to/local/folder # 指定本地文件系统里安装,可用这种方式来测试还未发布的包
yarn add file:/path/to/local/tarball.tgz # 指定本地文件系统里安装压缩包
yarn add <git remote url> # 指定远程 git repo 安装
yarn add <git remote url> # 指定远程 git repo 安装,可指定 git <branch/commit/tag>
yarn add https://x.y/package.tgz # 指定远程 gzipped 压缩包安装。
二进制文件
# 指定sass的二进制文件下载 sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ # 指定electron的二进制文件下载 electron_mirror=https://npm.taobao.org/mirrors/electron/ 类似还有 --canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas
代理
更新依赖
yarn upgrade-interactive --latest
PNP
Yarn 2+
.pnp.cjs
使用.pnp.cjs替代node_modules来帮助node解析包.
dlx
缓存
/home/kangkk
yarn cache clean
全局缓存
/home/kangkk
yarn cache clean --mirror
全局缓存
硬链接
编辑器支持
/home/kangkk
yarn dlx @yarnpkg/sdks vscode
命令
policies
在存储库中签入 Yarn 版本。该命令只会从 GitHub 存储库下载单文件版本,将其存储在项目的 .yarn/releases 文件夹中。
/home/kangkk
yarn policies set-version <version>
yarn policies set-version # 下载最新的稳定版本 yarn policies set-version --rc # 下载最新的 rc 版本 yarn policies set-version 1.13.0 # 下载特定版本 yarn policies set-version '^1.12.0' # 下载最新的次要 yarn policies set-version berry
升级到最新发布版本
/home/kangkk
yarn set version latest
版本升级
从版本 1 升级到版本 4
/home/kwy
corepack enable \
&& corepack prepare yarn@4.9.2 --activate
插件
/home/kangkk
yarn plugin import workspace-tools
workspaces
package.json
"workspaces": ["athena-frontend"]
查看包含的项目及依赖关系
/home/kangkk
yarn workspaces list --json
在所有 workspaces 包含的所有项目下执行命令
/home/kangkk
yarn workspaces foreach run build
在所有 workspaces 包含的一个项目下执行命令
/home/kangkk
yarn workspace <workspaceName> <commandName>
子项目
package.json
...
"packageManager": "yarn@4.4.1",
"workspaces": [
"athena-frontend",
"src/next/codemirror/language/test/lezer-test"
]
}
workspaces 中可以是子项目 package.json 的名称或者是相对路径,依据经验而言,对于复杂项目(子项目位置不统一)的场景,相对路径是更好的选择。
No categories