解决 yarn.ps1 cannot be loaded because the execution of scripts is disabled on this system 错误

2026-06-09 284 浏览 0 评论

今天使用 yarn 安装 ejs 模板引擎,运行命令后却得到了这个错误:

File C:\Program Files\nodejs\yarn.ps1 cannot be loaded because the
execution of scripts is disabled on this system.
Please see "get-help about_signing" for more details.
At line:1 char:5
+ yarn <<<<  add egg-view-ejs
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

原因

原来是命令行默认不允许执行 *.ps1 脚本文件。运行 ps1 文件会得到上面的错误,可以通过 Get-ExecutionPolicy,来取得当前策略,用 Set-ExecutionPolicy 设置当前策略。

解决方法

下面的命令可以解决上面的错误:

Set-ExecutionPolicy RemoteSigned

上面的代码我在执行的手,什么都没有输出,但是在此安装 npm 包的时候,就一切正常了:

Policy 的有效参数

  • -- Restricted:  不载入任何配置文件,不运行任何脚本。 "Restricted" 是默认的。
  • -- AllSigned: 只有被 Trusted publisher 签名的脚本或者配置文件才能使用,包括你自己再本地写的脚本
  • -- RemoteSigned:  对于从 Internet 上下载的脚本或者配置文件,只有被 Trusted publisher 签名的才能使用。
  • -- Unrestricted: 可以载入所有配置文件,可以运行所有脚本文件. 如果你运行一个从 internet 下载并且没有签名的脚本,在运行之前,你会被提示需要一定的权限。
  • -- Bypass: 所有东西都可以使用,并且没有提示和警告。
  • -- Undefined: 删除当前 scope 被赋予的 Execution Policy.  但是 Group Policy scope 的 Execution Policy 不会被删除。

发布评论

发布评论前请先 登录
0 评论
点赞
收藏

评论列表 0

暂无评论