如何修复错误 - nodemon.ps1无法加载,因为在此系统上禁用了运行脚本(没有安全风险)



终端上的错误:无法加载nodemon.ps1,因为在此系统上禁用了运行脚本。欲了解更多信息 信息,请参阅about_Execution_Policieshttps://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

我有一个解决方案可以通过以下方式解决问题:

1. Open Windows PowerShell with Run as Administrator2. Run this command: Set-ExecutionPolicy Unrestricted

这解决了问题,但这样系统会显示安全风险警告.
我的问题:有没有其他方法可以在没有安全风险的情况下解决这个问题?目标是使用nodemon

我建议使用RemoteSigned而不是Unrestricted,如果可能的话,将策略限制为CurrentUser

以管理员身份运行 Powershell,然后:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

RemoteSigned:"Windows 服务器计算机的默认执行策略。



除此之外,我不会太担心它,因为它不打算成为一种安全机制。请参阅文档中的这句话:

"执行策略不是限制用户的安全系统 行动。例如,用户可以通过键入 无法运行脚本时命令行中的脚本内容。 相反,执行策略可帮助用户设置基本规则和 防止他们无意中侵犯他们。

对于那些不知道如何使用Windows PowerShell解决此错误的人

  1. 打开 PowerShell(以管理员身份运行)
  2. 使用此命令检查当前执行策略
Get-ExecutionPolicy
# You should get 'Restricted'
  1. 运行此命令以使其"不受限制">
Set-ExecutionPolicy Unrestricted
  1. 再次检查执行策略是否通过运行此命令更改
Get-ExecutionPolicy
# You should get 'Unrestricted'
  1. 现在尝试在你的项目上运行nodemon。
nodemon 'filename.js'

希望这会有所帮助

步骤 1 : 转到此位置 -->文件 C:\Users\Dell\AppData\Roamingpm
Step 2 : 删除 nodemon.ps1 文件并运行命令。

允许远程签名脚本在本地计算机上运行没有任何安全风险。这基本上意味着您可以执行本地未签名脚本,即由您编写的脚本,而来自远程源(在本例中为 nodemon)的脚本必须由受信任的机构签名。

PS:如果你在Windows上,你可以转到开发人员的设置>>更新和安全>>,>>选中旁边的框change execution policy to allow local powershell scripts to run without signing

此命令可能会有所帮助

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

我有一个更好的解决方案,只需转到nodemon.ps1所在的文件夹(路径错误)并删除nodemon.ps1文件。

现在转到终端并运行nodemon -v,如果您获得版本,则问题已解决。

在使用 npm 时,我建议桥接scripts部分中的 nodemon 命令,并保持安全设置不变。

package.json:

{
"scripts": {
"hmr": "nodemon index.js",
"nodemon": "npm run hmr",
"start": "node index.js"
},
"dependencies": {}
}

只需执行npm run nodemon,它将在幕后运行hmr行。

您可以在不更改策略的情况下执行此操作。 C:\Users\HP MICROTECH\AppData\Roamingpmodemon.ps1 (路径在错误屏幕上给出)

只需转到此路径并删除 nodemon.ps1

你的节点在那里运行。

消除此错误的最佳方法是
以管理员身份在 Windows Power shell 中运行以下命令

设置-执行策略远程签名 -范围 当前用户

基本上它会要求您更改执行策略,然后键入 Y .

**OR**

您可以做的是无需更改执行策略

  1. 使用以下命令安装 nodemon: npm i nodemon -g
  2. 然后键入节点 -V 您将看到错误第一行中的路径转到该路径,然后将其删除 nodemon.ps1 文件或扩展名为 .ps1xml 的文件名。

确保您正在全局安装 nodemon 有关更多详细信息,微软执行策略文档

此cmd工作正常。

设置-执行策略远程签名 -范围 当前用户

运行某些节点命令时遇到类似问题。 无法加载 AppData\Roamingpm\serverless.ps1,因为在此系统上禁用了运行脚本。

PS C:Usersadas67> Get-ExecutionPolicy
Restricted
PS C:Usersadaaa> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy       Undefined
UserPolicy       Undefined
Process       Undefined
CurrentUser       Undefined
LocalMachine       Undefined

由此解决。

设置-执行策略 -执行策略 远程签名 -范围 当前用户

转到文件夹: - C:\Users\admin\AppData\Roamingpm

删除 nodemon 或 nodemon.ps1 的 powershell 文件。 你会很高兴去的。

运行以下命令:

  1. set-ExecutionPolicy RemoteSigned -scope CurrentUser
  2. 获取执行策略
  3. 获取执行策略列表

转到此链接,然后按照步骤操作。

以管理员身份打开 Powershell,然后运行以下命令:

PS> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
PS> Get-ExecutionPolicy
RemoteSigned
PS> .Start-ActivityTracker.ps1
.Start-ActivityTracker.ps1 : File .Start-ActivityTracker.ps1 cannot be loaded.
The file .Start-ActivityTracker.ps1 is not digitally signed.
The script will not execute on the system.
For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .Start-ActivityTracker.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
PS> Unblock-File -Path [Your Path where the problem found]
PS> Get-ExecutionPolicy
RemoteSigned

在窗口 10 中。

在管理和 npm 的系统变量中映射了错误的路径。转到"环境变量",然后更改"变量PATH下的路径。

第 1 步:

C:Program Filesnodejsnode_modulesnpmC:UsersAdministratorAppDataRoamingnpm

第 2 步:

然后重新启动我的系统。

它工作正常。

nodemon:无法加载文件 C:\Users\HP\AppData\Roamingpmodemon.ps1,因为在此系统上禁用了运行脚本。有关更多信息
information,about_Execution_Policies请参阅 https:/go.microsoft.com/fwlink/?链接 ID = 135170。

如果有人面临这个问题...所以我的意见是,只需进入文件并删除 nodemon.ps1 文件,然后尝试使用 nodemon 文件名

我用以下命令解决了我的问题。

=>以管理员身份运行PowerShell

=>运行以下命令

get-ExecutionPolicy

=>如果受到限制;运行以下命令

set-ExecutionPolicy Unrestricted 

=>并设置为全部

=>再次通过 foll 命令检查。

get-ExecutionPolicy

它将不受限制,问题得到解决

这解决了我的问题。

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

也许这更安全,因为仅限于当前用户。

为了避免以后出现任何问题 migt

  1. 转到您的 package.json 文件
  2. 改变
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},

到这样的东西

"scripts": {
"start": "node server/app.js",
"dev": "nodemon server/app.js"
},

此处的服务器必须是你的应用.js本地化的文件夹

最新更新