Windows11 安装 NodeJS 报错处理

4 阅读1分钟

PowerShell 下查询 npm 版本报错

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

通常是因为 Windows 默认禁止执行脚本

处理

右键点击“开始”菜单,选择“Windows PowerShell”或“Windows PowerShell (管理员)”。 ‌设置执行策略‌:

在 PowerShell 窗口中,输入以下命令来设置执行策略,允许运行本地脚本:

PS C:\Users\xxx> Set-ExecutionPolicy RemoteSigned

这将允许运行本地脚本和从 Internet 下载的签名脚本。如果你完全信任你的系统环境,也可以使用 Unrestricted,但请注意这可能会带来安全风险。

注意:必须以管理员角色打开“Windows PowerShell”,否则会报错

PS C:\Users\xxx\Desktop> Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy : 对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”的
访问被拒绝。 要更改默认(LocalMachine)作用域的执行策略,请使用“以管理员身份运行”选项启动 Windows PowerShell。要更改当
前用户的执行策略,请运行 "Set-ExecutionPolicy -Scope CurrentUser"。
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyComma
   nd