“Npm:无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。”的解决方法

“npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。”的解决方法

  1.故障表现   Windows平台下,一般是首次安装Nodejs后,在终端、cmd或Powershell中运行npm时,会报错误“npm : 无法加载文件 D:\ProgramFiles\nodejs\npm.ps1,因为在此系统上禁止运行脚本。……”

npm error code ECONNRESET
npm error syscall read
npm error errno ECONNRESET
npm error network request to https://registry.npmjs.org/hexo-cli failed, reason: read ECONNRESET
npm error network This is a problem related to network connectivity.
npm error network In most cases you are behind a proxy or have bad network settings.
npm error network
npm error network If you are behind a proxy, please make sure that the
npm error network 'proxy' config is set properly.  See: 'npm help config'
npm error A complete log of this run can be found in: C:\Users\caomi\AppData\Local\npm-cache\_logs\2024-07-27T03_29_31_547Z-debug-0.log

  如下图: Img   2.故障原因   故障是由Powershell的执行策略(get-ExecutionPolicy)默认设置造成的。   3.解决方法 这里快速的解决方法是在管理员权限运行“set-executionpolicy remotesigned”→键入“Y”(是)或“A”(全是)即可。 Img   再试一下,就OK了。 Img

相关知识:

  1.Powerfull功能   PowerShell 执行策略是一项安全功能,用于控制 PowerShell 加载配置文件和运行脚本的条件。 此功能有助于防止恶意脚本的执行。   在非 Windows 计算机上,默认执行策略是 Unrestricted 且无法更改。 Set-ExecutionPolicy cmdlet 可用,但 PowerShell 会显示不支持的控制台消息。 虽然 Get-ExecutionPolicy 在非 Windows 平台上返回 Unrestricted,但该行为实际上与 Bypass 匹配,因为这些平台未实施 Windows 安全区域。   2.获取所有执行策略

Get-ExecutionPolicy -List
Scope          ExecutionPolicy
---
MachinePolicy  Undefined
UserPolicy     Undefined
Process        Undefined
CurrentUser    AllSigned
LocalMachine   Undefined

  3.设置执行策略

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
Get-ExecutionPolicy -List

Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       AllSigned
 LocalMachine    RemoteSigned

参考文献:

  1.Get-ExecutionPolicy   2.powershell–脚本运行权限政策及‘禁止执行脚本’解决方法

0%