powershell 里执行 npm 命令时,或是 Vscode 里用 powershell 执行 npm 命令的时候,不能在 powershell 里运行,而是弹出一个 git bash 窗口去运行。
因为powershell禁止未签名脚本和来自其他用户的签名脚本运行,首先确定是否开启了这个功能。
输入:get-ExecutionPolicy
系统回复 Restricted,表示状态是禁止的。
系统回复 RemoteSigned ,表示开启了。
如果没开启用下面解决方法开启。
解决方法:
用管理员身份运行powershell
输入 set-ExecutionPolicy RemoteSigned
输入:y
如果开启了还是不好使,那么可以查看下
我们可以用 npm config list 看下 npm 配置便知道原因了
因为 script-shell = "C:\\Program Files\\Git\\git-bash.exe" 默认是 git-bash.exe。
我们可以把这个改成 powershell 执行命令:
npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"
也可以打开配置文件 C:\Users\hyn\.npmrc 修改配置。
再次执行 npm 命令时,就会在 powershell 里执行了。