什么是纱线?
Yarn 是一个包管理器,帮助开发 JavaScript 代码并通过软件包共享。 一个团队在 Facebook 将其开发为 Node 包管理器 (NPM) 客户端的替代方案,并专注于速度、安全性和一致性。 包,也称为模块,用于分发代码。 package.json 文件概述了包的内容和所有相关的分布式代码。
有多种方法可以在 Windows 服务器上安装 Yarn。 本教程提供了四种在 Windows Server 上安装 Yarn 的方法。
在 Windows Server 上安装 Yarn 的先决条件
- 安装了 Windows Server 的主机。
- Node.js 安装在服务器上。
- 在服务器上运行的 Windows PowerShell。
- 管理员访问服务器。
如何在 Windows Server 上安装 Yarn 的 4 种方法
以下是在 Windows 服务器上安装 Yarn 的 4 种方法。
通过 MSI 安装程序在 Windows Server 上安装 Yarn
第1步:
首先,您需要下载 Yarn 安装包。 下载 Yarn 官方网站上的 Yarn .msi 安装程序。
第2步:
找到并双击下载的包。 将出现一个消息框,您将在其中单击 跑.
第 3 步:
Yarn 安装设置会弹出一个新的欢迎向导。 点击 下一个.
第4步:
下一个屏幕显示许可协议详细信息。 单击窗口底部附近的复选框以接受许可协议,然后单击 下一个.

第 5 步:
要完成 Yarn 包安装,请单击 安装.

第 6 步:
纱线安装完成后,将出现一个确认消息框。 点击 结束.

第 7 步:
现在,在 Windows PowerShell 上运行以下 Yarn 版本检查命令来验证 Yarn 包安装是否完成。
yarn --version
输出将显示版本号,表示已安装。
PS C:UsersAdministrator> yarn --version
1.22.18
PS C:UsersAdministrator>
如何使用 Chocolatey 安装 Yarn 包
巧克力味 是 Windows 的命令行包管理器和安装程序。 Chocolatey 包管理器会自动在服务器上安装 Yarn 包的所有依赖项。 它还会检查 Node.js 安装,如果不存在,它将立即安装。
第1步:
要开始安装,请以管理员身份打开 Windows PowerShell。
第2步:
运行以下命令验证执行状态。
Get-ExecutionPolicy
输出应类似于以下内容。
Windows PowerShell
Copyright (C) 2014 Microsoft Corporation. All rights reserved.
PS C:UsersAdministrator> Get-ExecutionPolicy
Unrestricted
PS C:UsersAdministrator>
在这个 example,Get-ExecutionPolicy 是 Unrestricted,这是预期的输出。 在某些情况下,输出会受到限制,您需要执行以下命令来更改它并完成安装。
Set-ExecutionPolicy AllSigned
类型 是 在提示符下启用不受限制的选项。
第 3 步:
在 PowerShell 上运行以下命令以完成安装。
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
这是输出。
PS C:UsersAdministrator>
PS C:UsersAdministrator> Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/1.1.0.
Downloading https://community.chocolatey.org/api/v2/package/chocolatey/1.1.0 to C:UsersADMINI~1AppDataLocalTemp2chocolateychocoInstallchocolatey.zip
Not using proxy.
Extracting C:UsersADMINI~1AppDataLocalTemp2chocolateychocoInstallchocolatey.zip to C:UsersADMINI~1AppDataLocalTemp2chocolateychocoInstall
Installing Chocolatey on the local machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:ProgramDatachocolatey'
WARNING: It's very likely you will need to close and reopen your shell
before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:ProgramDatachocolateylib'
(i.e. C:ProgramDatachocolateylibyourPackageName).
A shim file for the command line goes to 'C:ProgramDatachocolateybin'
and points to an executable in 'C:ProgramDatachocolateylibyourPackageName'.
Creating Chocolatey folders if they do not already exist.
WARNING: You can safely ignore errors related to missing log files when
upgrading from a version of Chocolatey less than 0.9.9.
'Batch file could not be found' is also safe to ignore.
'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
Attempting to locate it from bootstrapper.
PATH environment variable does not have C:ProgramDatachocolateybin in it. Adding...
WARNING: Not setting tab completion: Profile file does not exist at
'C:UsersAdministratorDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
first prior to using choco.
Ensuring Chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
PS C:UsersAdministrator>
第4步:
Chocolatey 安装完成后,通过运行以下命令在 Windows 服务器上安装 Yarn 包。
choco install yarn
这是输出。 你需要输入 是 运行脚本并完成 Yarn 安装。
PS C:UsersAdministrator> choco install yarn
Chocolatey v1.1.0
Installing the following packages:
yarn
By installing, you accept licenses for the packages.
Progress: Downloading yarn 1.22.15... 100%
yarn v1.22.15 [Approved]
yarn package files install completed. Performing other installation steps.
The package yarn wants to run 'chocolateyinstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): Y
这是输出以及 Yarn 已安装的确认信息。
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): Y
Downloading yarn
from 'https://yarnpkg.com/downloads/1.22.15/yarn-1.22.15.msi'
Progress: 100% - Completed download of C:UsersAdministratorAppDataLocalTempchocolateyyarn1.22.15yarn-1.22.15.msi (1.6 MB).
Download of yarn-1.22.15.msi (1.6 MB) completed.
Hashes match.
Installing yarn...
yarn has been installed.
第 5 步:
要确认 Yarn 安装正确,请检查 Windows 命令提示符 (CMD) 中的版本。 访问它的一种快速方法是通过“运行”菜单。 请按 视窗+R 键和类型 命令 在提示下。

运行以下命令检查 Yarn 版本。
yarn -version
输出应该包含一个 Yarn 版本。
Microsoft Windows [Version 10.0.17763.2686]
(c) 2018 Microsoft Corporation. All rights reserved.
C:UsersAdministrator>yarn --version
1.22.15
C:UsersAdministrator>
使用 Scoop 安装 Yarn 包
舀 是另一个用于 Windows 的命令行包管理器和安装程序。 这些步骤将指导您完成 Scoop 和 Yarn 的安装。
第1步:
以管理员身份打开 PowerShell,然后运行以下命令。 它将设置执行策略以允许 PowerShell 运行本地脚本。
set-executionpolicy remotesigned -scope currentuser
这是输出。
PS C:UsersAdministrator> set-executionpolicy remotesigned -scope currentuser
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
PS C:UsersAdministrator>
PS C:UsersAdministrator>
第2步:
使用以下命令安装 Scoop。
iwr -useb get.scoop.sh -outfile 'install.ps1'
然后从 PowerShell 运行以下命令,以管理员身份执行脚本。
.install.ps1 -RunAsAdmin
一旦我们运行该命令,它将开始安装,如下所示。
PS C:UsersAdministrator> iwr -useb get.scoop.sh -outfile 'install.ps1'
PS C:UsersAdministrator> .install.ps1 -RunAsAdmin
Initializing...
Downloading...
Extracting...
Creating shim...
Adding ~scoopshims to your path.
Scoop was installed successfully!
Type 'scoop help' for instructions.
PS C:UsersAdministrator>
第 3 步:
接下来,使用 scoop 命令安装 Yarn。
scoop install yarn
这是安装输出。
PS C:UsersAdministrator> scoop install yarn
Installing 'yarn' (1.22.18) [64bit]
yarn-v1.22.18.tar.gz (1.2 MB) [===============================================================================] 100%
Checking hash of yarn-v1.22.18.tar.gz ... ok.
Extracting yarn-v1.22.18.tar.gz ... done.
Linking ~scoopappsyarncurrent => ~scoopappsyarn1.22.18
Persisting cache
Persisting global
Persisting mirror
Running post-install script...
yarn config v1.22.18
success Set "cache-folder" to "C:UsersAdministratorscoopappsyarncurrentcache".
Done in 0.11s.
yarn config v1.22.18
success Set "yarn-offline-mirror" to "C:UsersAdministratorscoopappsyarncurrentmirror".
Done in 0.07s.
yarn config v1.22.18
success Set "global-folder" to "C:UsersAdministratorscoopappsyarncurrentglobal".
Done in 0.07s.
yarn config v1.22.18
success Set "prefix" to "C:UsersAdministratorscoopappsyarncurrentglobal".
Done in 0.07s.
'yarn' (1.22.18) was installed successfully!
PS C:UsersAdministrator>
如果尚未安装 Node.js,您可以使用以下 scoop 命令进行安装。
scoop install nodejs
这是输出。
PS C:UsersAdministrator> scoop install nodejs
Installing '7zip' (21.07) [64bit]
7z2107-x64.msi (1.8 MB) [=====================================================================================] 100%
Checking hash of 7z2107-x64.msi ... ok.
Extracting 7z2107-x64.msi ... done.
Linking ~scoopapps7zipcurrent => ~scoopapps7zip21.07
Creating shim for '7z'.
Creating shortcut for 7-Zip (7zFM.exe)
Persisting Codecs
Persisting Formats
Running post-install script...
'7zip' (21.07) was installed successfully!
Notes
-----
Add 7-Zip as a context menu option by running: "C:UsersAdministratorscoopapps7zipcurrentinstall-context.reg"
Installing 'nodejs' (17.9.0) [64bit]
node-v17.9.0-win-x64.7z (16.6 MB) [===========================================================================] 100%
Checking hash of node-v17.9.0-win-x64.7z ... ok.
Extracting node-v17.9.0-win-x64.7z ... done.
Linking ~scoopappsnodejscurrent => ~scoopappsnodejs17.9.0
Persisting bin
Persisting cache
Running post-install script...
'nodejs' (17.9.0) was installed successfully!
PS C:UsersAdministrator>
第4步:
通过使用此命令检查版本,确保 Yarn 已正确安装在服务器上。
yarn --version
它显示版本号,如下所示。
PS C:UsersAdministrator> yarn --version
1.22.18
PS C:UsersAdministrator>
如何使用 NPM 安装 Yarn
NPM 预装了 Node.js。 它主要用于创建和共享 JavaScript 代码,并提供了一种不同的方式来安装 Yarn。 按照以下步骤使用 NPM 安装 Yarn。
第1步:
以管理员身份打开 PowerShell 并运行以下命令以确认 NPM 已安装在服务器上。
npm -version
输出应显示版本。
PS C:UsersAdministrator> npm --version
8.5.5
PS C:UsersAdministrator>
第2步:
使用以下命令安装 Yarn。
npm install --global yarn
输出如下所示。
PS C:UsersAdministrator> npm install --global yarn
added 1 package, and audited 2 packages in 5s
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.5.5 -> 8.6.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.6.0
npm notice Run npm install -g [email protected] to update!
npm notice
PS C:UsersAdministrator>
第 3 步:
要检查 Yarn 是否正确安装,请使用以下命令检查包版本。
yarn -version
输出显示版本。
PS C:UsersAdministrator> yarn --version
1.22.18
PS C:UsersAdministrator>
可能用于各种任务的通用纱线命令
获取 yarn 命令详细信息列表的最佳方法是使用帮助选项。 下面是在终端中使用的语法。
yarn help
使用以下命令启动一个新项目。
yarn init
为了安装 Yarn 项目的所有依赖项,请使用以下两个命令之一。
yarn
或者
yarn install
为了向项目添加单个依赖项,请使用以下语法,将 package 替换为实际包,将 version 替换为依赖项版本,并将 tag 替换为依赖项的适当标记。
yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
要将依赖项添加到特定类别,我们可以使用以下三个选项之一:
- 开发依赖。
- 对等依赖项。
- 可选的依赖项。
添加这些命令依赖项的语法如下。
yarn add [package] --dev
yarn add [package] --peer
yarn add [package] --optional
如果要升级现有依赖项,请使用 yarn upgrade 命令。
yarn upgrade [packagename]
yarn upgrade [packagename]@[version]
yarn upgrade [packagename]@[tag]
yarn remove 命令用于删除现有的依赖项。
yarn remove [package]
要将 Yarn 升级到最新版本,请运行以下命令之一。
yarn set version latest
yarn set version from sources
总结
使用本指南可帮助您了解如何在 Windows 服务器上安装 Yarn。 Yarn 可帮助您快速、安全、一致地管理包和分发代码。
如果您想让您的 Windows 服务器正常运行,Liquid Web 为我们的 VPS 托管提供 Windows 选项, Cloud 专用服务器和专用服务器。 联系我们的销售团队进行设置。