什麼是線程?

Yarn 是 JavaScript 的替代 npm 客戶端,作為 Facebook 團隊開發的包管理器。您還可以與世界各地的其他開發人員共享您的代碼並使用他們的解決方案。代碼本身通過包分發,有時稱為模塊。一個包包含一個描述內容和所有相關分發代碼的 .package.json 文件。 與 npm 相比,Yarn 被認為是更好的替代品,並且在以下參數上優於 NPM:

  • 與標準客戶端相比,Yarn 在我們的比較和測試中快了 2-3 倍。
  • 安裝可靠性。提高可靠性有兩個原因。
    • 短期的計算機網絡問題可能會導致無法接收數據包。
    • 當一個包的新版本發佈時,它可能會做出與其他包不兼容的更改並破壞您的應用程序。 Yarn 解決了這兩個問題並輕鬆解決。
  • Yarn 也兼容 Npm 和 Bower。

先決條件

在安裝 Yarn 之前必須安裝 Node.js。 您可以使用 Windows 終端或 PowerShell 驗證系統上是否安裝了 node.js。

1. 按 Win+R 打開終端並輸入 cmd。

2. 使用命令行,您可以使用以下命令驗證是否安裝了 Node.js:

C:Windowssystem32>node -v‘
 node’ is not recognized as an internal or external command, operable program or batch file.
 C:Windowssystem32>

如果您沒有安裝 node.js,請轉到主要的 node.js 網站並 下載安裝程序.

線程安裝3

然後你必須運行安裝程序。

線程安裝4

現在您必須接受許可協議。

螺紋安裝 6

現在,保留所有默認設置並[次へ]單擊以安裝 node.js。

線程安裝 5

最後點擊“Finish”完成安裝。現在您可以在終端中使用以下命令重新運行該命令並驗證 node.js 是否已正確安裝:

C:Windowssystem32>node --version
 V14.3.0
 C:Windowssystem32 

螺紋安裝

微星安裝

我用來安裝 Yarn 的第一種方法是 下載 Yarn .msi 安裝程序紗線官網 將其安裝在本地計算機上。

螺紋安裝 7

然後運行 .msi 安裝程序。

螺紋安裝 8

然後接受許可協議。

螺紋安裝 9

下一個,[インストール]點擊。

線程安裝 10

最後,單擊“完成”按鈕。

線程安裝 11

您可以使用以下 CLI 命令驗證是否安裝了 Yarn:

C:Windowssystem32>yarn --version
 1.22.4
 C:Windowssystem32>

巧克力裝置

安裝 Yarn 的另一種方法是使用適用於 Windows 的 Chocolatey 包管理器。這個方法很有用,因為 Chocolatey 會檢測 node.js 是否已經安裝,如果沒有安裝它會自動安裝。作為管理員做所有事情很重要。是這裡 Chocolatey 安裝說明.

設置 Chocolatey 後,您可以使用以下命令安裝 Yarn:

C:Windowssystem32>choco install yarn
 Chocolatey v0.10.15
 Installing the following packages:
 yarn
 By installing you accept licenses for the packages.
 
 yarn v1.22.4 [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
 
 Downloading yarn
   from 'https://yarnpkg.com/downloads/1.22.4/yarn-1.22.4.msi'
 Progress: 100% - Completed download of C:UsersDmitryAppDataLocalTempchocolateyyarn1.22.4yarn-1.22.4.msi (1.57 MB).
 Download of yarn-1.22.4.msi (1.57 MB) completed.
 Hashes match.
 Installing yarn...
 yarn has been installed.
 #< CLIXML
 <Objs Version="1.1.0.1" xmlns="https://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><Obj S="progress" RefId="1"><TNRef RefId="0" /><MS><I64 N="SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><S S="debug">Host version is 5.1.18362.752, PowerShell Version is '5.1.18362.752' and CLR Version is '4.0.30319.42000'.</S><S S="verbose">ing function 'Format-FileSize'.</S><S S="verbose">ing function 'Get-ChecksumValid'.</S><S S="verbose">ing function 'Get-ChocolateyUnzip'.</S><S S="verbose">ing function 'Get-ChocolateyWebFile'.</S><S S="verbose">ing function 'Get-EnvironmentVariable'.</S><S S="verbose">ing function 'Get-EnvironmentVariableNames'.</S><S S="verbose">ing function 'Get-FtpFile'.</S><S S="verbose">ing function 'Get-OSArchitectureWidth'.</S><S S="verbose">ing function 'Get-PackageParameters'.</S><S S="verbose">ing function 'Get-PackageParametersBuiltIn'.</S><S S="verbose">ing function 'Get-ToolsLocation'.</S><S S="verbose">ing function 'Get-UACEnabled'.</S><S S="verbose">ing function 'Get-UninstallRegistryKey'.</S><S S="verbose">ing function 'Get-VirusCheckValid'.</S><S S="verbose">ing function 'Get-WebFile'.</S><S S="verbose">ing function 'Get-WebFileName'.</S><S S="verbose">ing function 'Get-WebHeaders'.</S><S S="verbose">ing function 'Install-BinFile'.</S><S S="verbose">ing function 'Install-ChocolateyDesktopLink'.</S><S S="verbose">ing function 'Install-ChocolateyEnvironmentVariable'.</S><S S="verbose">ing function 'Install-ChocolateyExplorerMenuItem'.</S><S S="verbose">ing function 'Install-ChocolateyFileAssociation'.</S><S S="verbose">ing function 'Install-ChocolateyInstallPackage'.</S><S S="verbose">ing function 'Install-ChocolateyPackage'.</S><S S="verbose">ing function 'Install-ChocolateyPath'.</S><S S="verbose">ing function 'Install-ChocolateyPinnedTaskBarItem'.</S><S S="verbose">ing function 'Install-ChocolateyPowershellCommand'.</S><S S="verbose">ing function 'Install-ChocolateyShortcut'.</S><S S="verbose">ing function 'Install-ChocolateyVsixPackage'.</S><S S="verbose">ing function 'Install-ChocolateyZipPackage'.</S><S S="verbose">ing function 'Install-Vsix'.</S><S S="verbose">ing function 'Set-EnvironmentVariable'.</S><S S="verbose">ing function 'Set-PowerShellExitCode'.</S><S S="verbose">ing function 'Start-ChocolateyProcessAsAdmin'.</S><S S="verbose">ing function 'Test-ProcessAdminRights'.</S><S S="verbose">ing function 'Uninstall-BinFile'.</S><S S="verbose">ing function 'Uninstall-ChocolateyEnvironmentVariable'.</S><S S="verbose">ing function 'Uninstall-ChocolateyPackage'.</S><S S="verbose">ing function 'Uninstall-ChocolateyZipPackage'.</S><S S="verbose">ing function 'Update-SessionEnvironment'.</S><S S="verbose">ing function 'Write-ChocolateyFailure'.</S><S S="verbose">ing function 'Write-ChocolateySuccess'.</S><S S="verbose">ing function 'Write-FileUpdateLog'.</S><S S="verbose">ing function 'Write-FunctionCallLogMessage'.</S><S S="verbose">ing alias 'Get-ProcessorBits'.</S><S S="verbose">ing alias 'Get-OSBitness'.</S><S S="verbose">ing alias 'Get-InstallRegistryKey'.</S><S S="verbose">ing alias 'Generate-BinFile'.</S><S S="verbose">ing alias 'Add-BinFile'.</S><S S="verbose">ing alias 'Start-ChocolateyProcess'.</S><S S="verbose">ing alias 'Invoke-ChocolateyProcess'.</S><S S="verbose">ing alias 'Remove-BinFile'.</S><S S="verbose">ing alias 'refreshenv'.</S><S S="verbose">ing function 'Format-FileSize'.</S><S S="verbose">ing function 'Get-ChecksumValid'.</S><S S="verbose">ing function 'Get-ChocolateyUnzip'.</S><S S="verbose">ing function 'Get-ChocolateyWebFile'.</S><S S="verbose">ing function 'Get-EnvironmentVariable'.</S><S S="verbose">ing function 'Get-EnvironmentVariableNames'.</S><S S="verbose">ing function 'Get-FtpFile'.</S><S S="verbose">ing function 'Get-OSArchitectureWidth'.</S><S S="verbose">ing function 'Get-PackageParameters'.</S><S S="verbose">ing function 'Get-PackageParametersBuiltIn'.</S><S S="verbose">ing function 'Get-ToolsLocation'.</S><S S="verbose">ing function 'Get-UACEnabled'.</S><S S="verbose">ing function 'Get-UninstallRegistryKey'.</S><S S="verbose">ing function 'Get-VirusCheckValid'.</S><S S="verbose">ing function 'Get-WebFile'.</S><S S="verbose">ing function 'Get-WebFileName'.</S><S S="verbose">ing function 'Get-WebHeaders'.</S><S S="verbose">ing function 'Install-BinFile'.</S><S S="verbose">ing function 'Install-ChocolateyDesktopLink'.</S><S S="verbose">ing function 'Install-ChocolateyEnvironmentVariable'.</S><S S="verbose">ing function 'Install-ChocolateyExplorerMenuItem'.</S><S S="verbose">ing function 'Install-ChocolateyFileAssociation'.</S><S S="verbose">ing function 'Install-ChocolateyInstallPackage'.</S><S S="verbose">ing function 'Install-ChocolateyPackage'.</S><S S="verbose">ing function 'Install-ChocolateyPath'.</S><S S="verbose">ing function 'Install-ChocolateyPinnedTaskBarItem'.</S><S S="verbose">ing function 'Install-ChocolateyPowershellCommand'.</S><S S="verbose">ing function 'Install-ChocolateyShortcut'.</S><S S="verbose">ing function 'Install-ChocolateyVsixPackage'.</S><S S="verbose">ing function 'Install-ChocolateyZipPackage'.</S><S S="verbose">ing function 'Install-Vsix'.</S><S S="verbose">ing function 'Set-EnvironmentVariable'.</S><S S="verbose">ing function 'Set-PowerShellExitCode'.</S><S S="verbose">ing function 'Start-ChocolateyProcessAsAdmin'.</S><S S="verbose">ing function 'Test-ProcessAdminRights'.</S><S S="verbose">ing function 'Uninstall-BinFile'.</S><S S="verbose">ing function 'Uninstall-ChocolateyEnvironmentVariable'.</S><S S="verbose">ing function 'Uninstall-ChocolateyPackage'.</S><S S="verbose">ing function 'Uninstall-ChocolateyZipPackage'.</S><S S="verbose">ing function 'Update-SessionEnvironment'.</S><S S="verbose">ing function 'Write-ChocolateyFailure'.</S><S S="verbose">ing function 'Write-ChocolateySuccess'.</S><S S="verbose">ing function 'Write-FileUpdateLog'.</S><S S="verbose">ing function 'Write-FunctionCallLogMessage'.</S><S S="verbose">ing alias 'Get-ProcessorBits'.</S><S S="verbose">ing alias 'Get-OSBitness'.</S><S S="verbose">ing alias 'Get-InstallRegistryKey'.</S><S S="verbose">ing alias 'Generate-BinFile'.</S><S S="verbose">ing alias 'Add-BinFile'.</S><S S="verbose">ing alias 'Start-ChocolateyProcess'.</S><S S="verbose">ing alias 'Invoke-ChocolateyProcess'.</S><S S="verbose">ing alias 'Remove-BinFile'.</S><S S="verbose">ing alias 'refreshenv'.</S></Objs>
 0
 Only an exit code of non-zero will fail the package by default. Set
  `--failonstderr` if you want error messages to also fail a script. See
  `choco -h` for details.
  The install of yarn was successful.
   Software installed as 'msi', install location is likely default.
 
 Chocolatey installed 1/1 packages.
  See the log for details (C:ProgramDatachocolateylogschocolatey.log).
 
 C:Windowssystem32>

最後,您可以使用以下命令檢查您的紗線安裝:

C:Windowssystem32>yarn --version
 1.22.4
 C:Windowssystem32>

安裝勺子

第三種安裝 Yarn 的方法是使用 Windows 的 Scoop 命令行安裝程序。Scoop 像 Chocolatey 一樣工作,主要區別是 Chocolatey 意圖 安裝 如果您還沒有安裝它,請深入研究 node.js 不要做. 可以附勺子 用於 獨家新聞.

要使用 scoop 安裝 node.js,請運行以下命令:

scoop install nodejs

然後您可以使用以下命令安裝紗線:

scoop install yarn

通用紗線命令

查看許多 Yarn 命令的最佳方式是運行 help 命令。

yarn help

開始一個新項目

要創建新項目,請運行以下命令:

yarn init

安裝依賴項

要為您的項目安裝所有依賴項,請使用以下兩個命令之一:

yarn
yarn install

添加依賴項

要將依賴項添加到您的項目,請使用以下命令格式之一:

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]

將依賴項添加到具有不同依賴項的類

要將依賴項添加到特定類別,您可以使用以下三個選項之一:

  • 開發依賴
  • 對等依賴
  • 可選依賴項

為這些命令添加依賴項的語法是:

yarn add [package] --dev  # dev-dependency
yarn add [package] --peer # peer-dependency
yarn add [package] --optimal # optimal-dependency

升級依賴

要升級現有的依賴項,可以使用 yarn up 命令。

yarn up [packagename]
yarn up [packagename]@[version]
yarn up [packagename]@[tag]

刪除依賴項

要刪除現有依賴項,您應該使用以下命令:

yarn remove [package]

紗線升級

要將 Yarn 升級到最新版本,請運行以下命令之一:

yarn set version latest
yarn set version from sources

結論。

在本教程中,我們探討了 Yarn 是什麼、它的用途以及人們為什麼選擇 Yarn 作為他們的包管理器。 我使用 .msi 安裝程序和包管理器 Chocolatey 和 Scoop 驗證了 Yarn 安裝。我們推薦使用 Chocolatey,它更方便,因為它會自動安裝 node.js。

如果您對此安裝進行故障排除有任何問題,請立即致電 800.580.4985 或 和…聊天 或聯繫我們與知識淵博的系統管理員聊天!