无法在Windows 10上安装Yarn

5

我安装了 node v16.13.0、npm v8.1.0、java 版本 1.8.0_211,我还以为我也已经安装了 yarn,但是尝试使用 yarn 全局安装 expo-cli 时,遇到以下问题:

yarn : File C:\Program Files\nodejs\yarn.ps1 cannot be loaded. The file C:\Program Files\nodejs\yarn.ps1 is not
digitally signed. You cannot run this script on the current system. For more information about running scripts and
setting execution policy, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ yarn global add expo-cli
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

然后我尝试再次安装yarn,但是现在我已经完全无法获取它:

PS C:\Users\user> npm install -g yarn

changed 1 package, and audited 2 packages in 1s

found 0 vulnerabilities

但是运行yarn --version时,我得到了与之前相同的错误:

yarn : File C:\Program Files\nodejs\yarn.ps1 cannot be loaded. The file C:\Program Files\nodejs\yarn.ps1 is not
digitally signed. You cannot run this script on the current system. For more information about running scripts and
setting execution policy, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ yarn --version
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

你是在PowerShell上运行它吗?尝试在命令提示符上运行。 - crimson589
通过cmd显示了已安装的版本(C:\ Users \ spike> yarn --version 1.22.15),为什么我无法从powershell获取它? 但即使使用cmd或其他任何东西,我也无法使用yarn,必须更改我的系统环境中的路径。 - user11240162
3个回答

12

执行:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

5

我建议不要更改你的系统策略。

相反,请按照以下方式操作:

从目录C:\Users%username%\AppData\Roaming\npm\中删除yarn.ps1,然后尝试清除C:\Users%username%\AppData\Roaming\npm-cache\中的npm缓存。


1
我没有npm-cache文件夹。 - Alessandro Sassi
我觉得这是正确的答案。为了解决一个问题而改变安全策略是一件可怕的事情,遵循这些步骤让我成功解决了这个问题。 - hugmungus
在我的情况下,需要删除的文件是C:\Program Files\nodejs\yarn.ps1。在删除它之后(我将其重命名),yarn命令可以正常工作。 - undefined
1
我之前有一个 nvm,所以它在 C:\Users%username%\AppData\Roaming\nvm\v16.20.2\ 这个路径下。 - undefined

0
在您的 npm 安装目录下,将 yarn.ps1 重命名为 yarn_disabled.ps1。现在,每当您调用 yarn,系统将退回到不像 PowerShell 那样具有数字签名概念的 yarn.CMD
推荐使用此方法而不是更改执行策略。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接