运行index.js报错:系统禁用脚本执行,尝试./index.js 3000无效
解决PowerShell中nodemon执行被阻止的问题
问题描述
PS C:\Users\Leff Tubat\express-demo> nodemon index.js
nodemon : File C:\Users\Leff Tubat\AppData\Roaming\npm\nodemon.ps1 cannot
be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
- nodemon index.js
+ CategoryInfo : SecurityError: (:) [], PSSecurityException + FullyQualifiedErrorId : UnauthorizedAccess
尝试直接执行./index.js 3000也无法解决问题。
解决方案
这是Windows PowerShell的执行策略限制导致的,默认策略不允许运行未经签名的脚本,可按以下方式解决:
以管理员身份打开PowerShell,执行命令修改全局执行策略:
Set-ExecutionPolicy RemoteSigned按提示输入
Y确认更改,之后重新运行nodemon index.js即可正常启动。若不想全局修改策略,可临时允许当前会话运行脚本:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass该设置仅对当前PowerShell会话有效,关闭后自动恢复默认策略。
另外,直接执行./index.js 3000无效是因为Node.js脚本需要通过Node命令启动,正确命令应为:
node index.js 3000
内容的提问来源于stack exchange,提问作者Leff Tubat
相关产品推荐
相关产品推荐

