windows http-server 因为在此系统上禁止运行脚本

我想在windows中运行 http-server 安装方法:

npm install http-server -g

这个在linuxt和macOS中使用都是非常的方便的,但是在windows中运行缺报错了。

报错信息

http-server
http-server : 无法加载文件 C:\Users\itkey\AppData\Roaming\npm\http-server.ps1,因为在此系统上禁止运行脚本。有关详细信息
,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ http-server
+ ~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

原因分析

查了查之后发现是在计算机上启动 Windows PowerShell 时,执行策略很可能是 Restricted(默认设置)。

Restricted 执行策略不允许任何脚本运行。

AllSigned 和 RemoteSigned 执行策略可防止 Windows PowerShell 运行没有数字签名的脚本。

本主题说明如何运行所选未签名脚本(即使在执行策略为 RemoteSigned 的情况下),还说明如何对 脚本进行签名以便您自己使用。

有关 Windows PowerShell 执行策略的详细信息,请参阅 about_Execution_Policy。

想了解 计算机上的现用执行策略,打开PowerShell 然后输入

get-executionpolicy

在这里插入图片描述

解决办法

以管理员身份打开PowerShell 输入

set-executionpolicy remotesigned

windows http-server 因为在此系统上禁止运行脚本_第1张图片
这样就可以正常运行了。

参考

《about_Execution_Policies》
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2
《PowerShell:因为在此系统上禁止运行脚本,解决方法》
https://www.jianshu.com/p/4eaad2163567

你可能感兴趣的:(windows,windows)