npm : 无法加载文件 D:\nodejs\npm.ps1,因为在此系统上禁止运行脚本
使用 powershell 运行 npm config get prefix
出现错误提示:
npm : 无法加载文件 D:\nodejs\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?
LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ npm config get prefix
+ ~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
PowerShell的执行策略被设置为不允许运行脚本。
在 PowerShell 运行命令:
Get-ExecutionPolicy
返回当前的执行策略是 Restricted (有限的)。那么需要修改为其它的执行策略,如 Restricted 以外的 AllSigned(所有签名的)、RemoteSigned(远程签名的)、Unrestricted(无限制的)。
在 PowerShell 运行命令:
Set-ExecutionPolicy Unrestricted -Scope CurrentUser
这里修改当前用户的执行策略为 Unrestricted ,即允许所有脚本运行,无论是否签名。(如果要修改为整个系统,使用 -Scope Machine
参数,但需要管理员权限。
更改完之后,重新运行 npm config get prefix
正常。
注意:更改执行策略可能会影响系统的安全性,特别是将策略设置为Unrestricted时。