gpt4 book ai didi

nodemon command is not recognized in terminal for node js server(在节点js服务器的终端中未识别nodemon命令)

转载 作者:bug小助手 更新时间:2023-10-27 20:18:22 27 4
gpt4 key购买 nike



I am doing node.js server setup from https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens. I am new in node.js. I am installing npm install nodemon --save. But when I am run the server with this nodemon server.js.

In the terminal showing:

我正在从https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens.进行node.js服务器设置我是node.js的新手。我正在安装NPM安装nodemon--保存。但是当我使用这个节点服务器server.js运行服务器时。在航站楼中显示:



nodemon is not recognized as internal or external command, operable
program or batch file



enter image description here


node server.js command is working and started the server, But nodemon command is not working.

node server.js命令正在工作并启动了服务器,但nodemon命令不工作。


I am set up the node js server from https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens video.

我从https://scotch.io/tutorials/authenticate-a-node-js-api-with-json-web-tokens视频中设置了节点js服务器。


I don't know why it is not working I have tried some command for the install nodemon.

我不知道为什么它不工作,我已经尝试了一些命令来安装nodemon。


npm install -g nodemon 
npm install -g nodemon --save
npm install --save-dev nodemon
npm install -g nodemon@debug

npm install -g --force nodemon

I have seen one link I can´t install nodemon globally, "nodemon" not recognized, But I don't know how to set the path because of my project location in D drive.

我已经看到一个我不能在全球安装nodemon的链接,“nodemon”没有被识别,但我不知道如何设置路径,因为我的项目位置在D盘。


I want to run nodemon server.js. How can this be done?

我想运行nodemon server.js。如何做到这一点呢?


更多回答

Note to future readers: do not post images of text. Post the text as text with whatever formatting is appropriate. See here for why.

给未来读者的注意:不要张贴文字的图片。以任何适当的格式将文本发布为文本。请看这里了解原因。

See also: npmjs.com/package/nodemon; the last paragraph of the 'Installation' section.

另请参阅:npmjs.com/Package/nodemon;“安装”部分的最后一段。

优秀答案推荐

You need to install it globally

您需要在全球范围内安装



npm install -g nodemon
# or if using yarn
yarn global add nodemon


And then it will be available on the path (I see now that you have tried this and it didn't work, your path may be messed up)

然后它将在路径上可用(我现在知道你已经尝试过了,但它不起作用,你的路径可能会被搞砸)



If you want to use the locally installed version, rather than installing globally then you can create a script in your package.json

如果您希望使用本地安装的版本,而不是全局安装,则可以在您的Package.json中创建一个脚本



"scripts": {
"serve": "nodemon server.js"
},


and then use

然后使用



npm run serve


optionally if using yarn

可选地,如果使用纱线



# without adding serve in package.json
yarn run nodemon server.js
# with serve script in package.json
yarn run serve


npm will then look in your local node_modules folder before looking for the command in your global modules

然后,NPM将在您的本地节点模块文件夹中查找该命令,然后再在全局模块中查找该命令




  1. Install nodemon globally:



    C:\>npm install -g nodemon

  2. Get prefix:



    C:\>npm config get prefix


    You will get output like following in your console:



    C:\Users\Family\.node_modules_global


    Copy it.


  3. Set Path.

    Go to Advance System Settings → Environment Variable → Click New (Under User Variables) → Pop up form will be displayed → Pass the following values:



    variable name = path,
    variable value = Copy output from your console

  4. Now Run Nodemon:



    C:\>nodemon .



No need to install nodemon globally. Just run this npx nodemon <scriptname.js>. That's it.

不需要在全球安装nodemon。只需运行这个npx nodemon 。就这样。



First, write npm install --save nodemon
then in package.json write the followings

首先,编写npm install --save nodemon,然后在package.json中编写以下内容



"scripts": {
"server": "nodemon server.js"
},




then write

然后写下





npm run server


I was facing the same issue. I had installed nodemon as a dev-dependency and when I tried to start the server it gave the message that

我也面临着同样的问题。我已经安装了nodemon作为开发依赖项,当我尝试启动服务器时,它给出了这样的消息




nodemon is not recognized as internal or external command, operable
program or batch file




Then I installed it globally and tried to start the server and it worked!

然后我在全球范围内安装了它,并尝试启动服务器,它起作用了!



npm install -g nodemon


This may come to late, But better to say somthing :)

这可能为时已晚,但更好的说法是:)



If you don't want to install nodemon globbaly you can use npx, it installs the package at run-time and will behave as global package (keep in mind that it's just available at the moment and does not exist globally!).

如果您不想安装nodemon global baly,您可以使用npx,它在运行时安装程序包,并将作为全局程序包运行(请记住,它目前仅可用,并不存在于全局范围内!)。



So all you need is npx nodemon server.js.

因此,您所需要的只是npx nodemon server.js。





To use nodemon you must install it globally.

要使用nodemon,您必须全局安装它。



For Windows

适用于Windows




npm i -g nodemon


For Mac

对于Mac




sudo npm i -g nodemon


If you don't want to install it globally you can install it locally in your project folder by running command npm i nodemon . It will give error something like this if run locally:

如果您不想全局安装它,可以通过运行命令npm i nodemon将其安装在本地的项目文件夹中。如果在本地运行,它将显示如下错误:




nodemon : The term 'nodemon' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again.


To remove this error open package.json file and add

要删除此错误,请打开Package.json文件并添加




"scripts": {
"server": "nodemon server.js"
},



and after that just run command

在那之后只需运行命令




npm run server


and your nodemon will start working properly.

你的节点就会开始正常工作。



I had the same error a few minutes ago and this is how I've solved it:

几分钟前我也犯了同样的错误,我是这样解决的:


1. Install "nodemon" Globally

1.全局安装nodemon



npm install nodemon -g



2. Then you need to add the npm path to the environment variables

2.然后需要将NPM路径添加到环境变量中


To find the path do this in the terminal:

要查找路径,请在终端中执行以下操作:



npm config get prefix



You'll get the output that looks like this: C:\Users\user\AppData\Roaming\npm

您将得到如下所示的输出:C:\USERS\USER\AppData\Roaming\NPM


If you're not sure about how you can update environment variables on Windows, check this out: Here

如果您不确定如何在Windows上更新环境变量,请查看以下内容:


3. Run the app again with "nodemon"
Before you run the app, create a new terminal to make sure that the terminal recognises the changes in the environment variables.
Then run:
ex:

3.使用nodemon再次运行应用程序在运行应用程序之前,创建一个新的终端,以确保终端能够识别环境变量的变化。然后运行:例如:



nodemon server.js




Does it need to be installed globally? Do you need to be able to just run nodemon server.js ? If not, you could always just call it from your local project directory. Should be here:

是否需要在全球范围内安装?您需要能够只运行nodemon server.js吗?如果没有,您可以随时从本地项目目录中调用它。应该在这里:



node_modules/.bin/nodemon


I have fixed in this way

我已经用这种方式修好了



  1. uninstall existing local nodemon


    npm uninstall nodemon



  2. install it again globally.


    npm i -g nodemon





This line solved my problem in CMD:

这句话解决了我在CMD的问题:



npm install --save-dev nodemon


I tried installing the nodemon globally but that doesn't worked for me.
whenever i try to run it always shows me the error:

我试着在全球范围内安装节点,但对我来说不起作用。每当我尝试运行时,它总是显示错误:



nodemon : The term 'nodemon' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is
correct and try again.


2. I have found two solutions for this

2.我找到了两个解决方案



solution 1:

解决方案1:



What i have tried is to update the "scripts" in package.json file and there i have added

我尝试的是更新Package.json文件中的“脚本”,并在那里添加了



"server": "nodemon app.js"


above line of code and after that

在代码行上方和之后



npm run server


Soluton 2:

解决方案2:




  1. Press the Windows key.


  2. Type "Path" in the search box and select "Edit the system environment variables"


  3. Click on "Environment Variables" near the bottom.


  4. In the "System Variables" section double click on the "Path" variable.


  5. Click "New" on the right-hand side.


  6. Copy and paste this into the box (replace [Username]):




C:\Users[Username]\AppData\Roaming\npm

C:\USERS[用户名]\AppData\Roaming\NPM




  1. restart your terminal and VSCode.


  2. Then type nodemon app.js to run the nodemon




i applied solution 2 as we just need to run nodemon [filename.js]

我应用了解决方案2,因为我们只需要运行nodemon[filename.js]



It is better to install nodemon globally instead as dev dependency to the project.

最好是全局安装nodemon,而不是作为项目的开发依赖项。


npm install -g nodemon

Npm安装-g nodemon


Official NPM CDN: Link

NPM官方CDN:链接


This package is used to monitor changes in the javascript files and re run the npm start so that it is easy to dev purposes.

此程序包用于监控javascript文件中的更改并重新运行NPM Start,以便于开发。



Since node prefix is not in the PATH ENV variable , any of the globally installed modules are not getting recognized.
Please try this.
Open cmd prompt
npm config get prefix
append the resulting path to PATH env variable.
Now you should be able to run nodemon from any location.
try this link and follow it.fixing npm permissions
https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-default-directory-to-another-directory

由于节点前缀不在PATH ENV变量中,因此无法识别任何全局安装的模块。请尝尝这个。打开cmd prompt npm config get prefix将结果路径追加到PATH env变量。现在你应该可以在任何位置运行nodemon了。尝试此链接并按照它.fixing npm permissions https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-default-directory-to-another-directory



You can run your node app by simply typing nodemon
It First run index.js
You can put your entry point in that file easily.

您只需输入nodemon就可以运行您的节点应用程序,首先运行index.js,您可以轻松地将您的入口点放入该文件中。



If you have not installed nodemon then you first you have to install it by

如果您还没有安装nodemon,那么您首先必须通过



npm install -g nodemon


If you got any permission error then use

如果您遇到任何权限错误,请使用



sudo npm install -g nodemon


You can check nodemon exists or not by

您可以通过以下方式检查节点是否存在



nodemon -v


For me setting the path variables was enough for the solution:

对我来说,设置PATH变量就足以解决问题:


Step 1) Install nodemon globally using npm install -g nodemon

步骤1)使用NPM Install-g nodemon全局安装nodemon


enter image description here


Step 2) Set the ENVIRONMENT VARIABLES, by adding npm path the PATH variable

步骤2)通过将NPM PATH添加到PATH变量来设置环境变量


1) Open Control Panel, search for environment variable

1)打开控制面板,搜索环境变量


enter image description here


2) Click open the environment variable
enter image description here

2)点击打开环境变量


3) Create new variable NPM set it with the path of npm as appears from the nodemon installation cmd output (as seen from nodemon installation screenshot):

3)创建新变量npm,将其设置为nodemon安装命令输出所示的npm路径(如nodemon安装截图所示):


enter image description here


4) Now add NPM variable to the PATH variables:

4)现在将NPM变量添加到PATH变量:


enter image description here


Step 3) Close the 'cmd' and open a fresh one and type nodemon --version
enter image description here

步骤3)关闭‘cmd’并打开一个新的命令并输入nodemon--版本


Now we have the nodemon ready to use :)

现在,我们已准备好使用节点:)



This issue is also possible if running scripts is disabled on the system. In order to enable it:

如果在系统上禁用运行脚本,也可能出现此问题。要启用它,请执行以下操作:



  1. Open Windows PowerShell with Run as Administrator



  2. Execute:


    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser



  3. npm install -g nodemon



  4. nodemon app





First you need to install nodemon globally by using this command:
"npm i -g nodemon" (for Windows) and "sudo npm i -g nodemon" (for Mac)
then run
"nodemon app.js"
Now you will be fine.

首先,你需要使用以下命令全局安装nodemon:“npm i -g nodemon”(Windows)和“sudo npm i -g nodemon”(Mac),然后运行“nodemon app.js”,现在你就可以了。



Just had the same problem after creating a new user profile on my development machine.

在我的开发机器上创建新的用户配置文件后,我也遇到了同样的问题。



The problem was that I wasn't running the console (command prompt\powershell ISE) as admin.

问题是我没有以管理员身份运行控制台(命令提示符\PowerShell ISE)。



Running as admin solved this problem for me.

以管理员身份运行为我解决了这个问题。



Just install Globally

只需全局安装即可



 npm install -g  nodemon


It worked for me on Windows 10.

我在Windows10上也用得上。



nodemon app.js


The Set-ExecutionPolicy cmdlet's default execution policy is Restricted for Windows.
You can try installing nodemon by setting this policy to Unrestricted.

Set-ExecutionPolicy Servlet的默认执行策略为“Restricted for Windows”。您可以尝试通过将此策略设置为Unrestricted来安装nodemon。



execute command : Set-ExecutionPolicy Unrestricted
and then try installing nodemon and execute command: nodemon -v

执行命令:无限制地设置-ExecutionPolicy,然后尝试安装nodemon并执行命令:nodemon-v



Even I had the same issue, the following command worked for me.

即使我也有同样的问题,下面的命令对我也有效。


npm install -g nodemon


Just install it globally

只需在全球范围内安装它


npm install -g nodemon

And add this script in your package.json

并将此脚本添加到您的Package.json中


"scripts": {
"start": "nodemon server.js"
},

And then run this command to start your server.js using nodemon

然后运行以下命令,使用nodemon启动server.js


npm start


Run this command:

运行此命令:



npm install nodemon -g


Now it will install the nodemon but the problem with my case is that it is installing nodemon somewhere else.I added Nodejs path from (ProgramFiles(x86)) but that did not worked so i found another solution.

现在它将安装nodemon,但我的情况下的问题是,它是安装nodemon在其他地方。我添加了Nodejs路径从(ProgramFiles(x86)),但这并不起作用,所以我找到了另一个解决方案。




  • Run above command

  • There will be a path shown during installation where nodemon is installed,then
    [Kindly go to below link to see the path][1]

    在安装过程中会显示安装nodemon的路径,然后[请转到下面的链接查看路径][1]



    [1]: https://i.stack.imgur.com/ld2sU.png

    [1]:https://i.stack.imgur.com/ld2sU.png


  • Copy the path upto npm and set it to environment variable

  • Now try the below command,hopefully it will run

    现在尝试下面的命令,希望它能运行



      nodemon YourAppName.js



All above options are failed, I got the permanent solution for this.
Add below line in package.json under dependencies and run npm install. This will add nodemon package to node_modules and there you go, enjoy the coding.

以上所有选项都失败了,我找到了永久的解决方案。在依赖项下的Package.json中添加以下行,并运行NPM Install。这将把nodemon包添加到node_MODULES中,然后您就可以享受编码了。



"nodemon": "^1.17.*"


Try in your packge.json:
put "./node_modules/.bin/nodemon" instead of just "nodemon".
For me it works.

在您的Packge.json中试一试:放置“./node_模块/.bin/nodemon”,而不仅仅是“nodemon”。对我来说,这很管用。



Step 1: $ npm install nodemon --> install nodemon on your project

步骤1:$npm安装nodemon-->在您的项目上安装nodemon


Step 2: Add serve in script on package.json file as:

步骤2:将Serve in脚本添加到Package.json文件中,如下所示:


"scripts": {
"serve": "nodemon app.js" // you can change file name accordingly
}

Step 3: $ npm run serve

第3步:$NPM运行服务



The following worked for me on windows 11.

以下是我在Windows11上使用的方法。



  1. Type npm install in the terminal. (Within the same directory as the project)

  2. Then type npm run serve to run the application from the default browser.


enter image description here



I was having this issue using the terminal from vscode after installing globally and trying other solutions and the following worked:

在全局安装并尝试其他解决方案后,我从vscode使用终端时遇到了这个问题,以下方法奏效了:



  1. Open a new terminal outside of vscode (alt+r -> cmd -> enter)



  2. Set directory to the folder containing the express app.js:
    cd C:\Users\<username>\Desktop\<project-name>\express



  3. Run nodemon app.js




Should work

应该行得通



Remove nodemon because it's a dev dependency and use node instead of it.

删除nodemon,因为它是一个dev依赖项,并使用node代替它。



"scripts": {
"start": "node server.js"
},


This worked for me.

这对我很管用。


更多回答

What is not working? The script using local install?

什么不起作用?脚本使用本地安装?

Yaa. Ihave installed nodemon. I putted script in package.json file. And after run npm run serve. After all then run nodemon server.js but not working.

啊呀。我已经安装了节点。我将脚本放入了Package.json文件中。跑完之后,NPM跑发球。毕竟,然后运行nodemon server.js,但不起作用。

package.json: { "scripts": { "serve": "nodemon server.js" }, "name": "node-site", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "express": "^4.14.0", "nodemon": "^1.11.0" }, "devDependencies": { "gulp-nodemon": "^2.2.1", "nodemon": "^1.11.0" } }

Package.json:{“脚本”:{“Serve”:“nodemon server.js”},“name”:“node-site”,“Version”:“1.0.0”,“Description”:“”,“main”:“server.js”,“脚本”:{“test”:“ECHO\”错误:未指定测试\“&EXIT 1”},“作者”:“”,“许可证”:“ISC”,“依赖项”:{“express”:“^4.14.0”,“nodemon”:“^1.11.0”},“devDependency”:{“gup-nodemon”:“^2.2.1”,“nodemon”:“^1.11.0”}}

Can you clarify "not working"? Is there an error? Does nothing happen?

你能澄清一下“不工作”吗?有什么差错吗?什么都没发生吗?

It may be because you have two sets of scripts in there, you have to have all scripts in one scripts object

这可能是因为您有两组脚本,您必须将所有脚本放在一个脚本对象中

This works!! This is was I was looking for. Thank you

这管用!!这就是我要找的。谢谢

this worked for me. Actually i have windows and needed to fix the environment variables. Thanks :)

这对我很管用。实际上,我有Windows,需要修复环境变量。谢谢:)

This Works Too!!!!!!!! i was working on a ExpressJS Project. I was using npm scripts, Then i Started to search On Google. I found This Link on Google, I Just Use npx nodemon on my laptop. But i wanted to use only nodemon <ScriptName.js>, And Then i found This

这也行得通!我当时在做一个ExpressJS项目。我在使用NPM脚本,然后我开始在谷歌上搜索。我在谷歌上找到了这个链接,我只是在我的笔记本电脑上使用了npx nodemon。但是我只想使用nodemon,然后我发现了这个

This solution worked for me but I did not unserstand why npm did not work7

这个解决方案对我有效,但我不明白为什么NPM不起作用7

This worked! Thanks a lot. Just a query, nodemon prevents us from restarting the server file, but we still have to refresh the browser. Can the browser refresh on its own, like it does in angular ?

这招奏效了!非常感谢。这只是一个查询,nodemon阻止我们重新启动服务器文件,但我们仍然必须刷新浏览器。浏览器是否可以像ANGLE那样自动刷新?

If you still get the error after doing this, you could try deleting the node_modules folder and re-running npm i before running the app again.

如果在执行此操作后仍然出现错误,您可以尝试删除NODE_MODULES文件夹并在再次运行该应用程序之前重新运行NPM I。

Wrong. Your script should say { "server": "nodemon server.js" }

不对。您的脚本应该是{“服务器”:“nodemon server.js”}

this worked for my case, where I just want to run a simple js file. But the nodemon cli command was automated to nodemon.cmd .\yourfileName.js

这对我的情况很有效,我只想运行一个简单的js文件。但是nodemon cli命令被自动设置为nodemon.cmd.YourfileName.js

This is helpful for me to run my nodejs api from shell script. Great thanks!

这对我从外壳脚本运行NodeJS API很有帮助。非常感谢!

This works perfect. In my environment (Windows Server) with 4 programmers connected to the same server, coding through RDP. Thanks Saad.

这个很好用。在我的环境(Windows服务器)中,4个程序员连接到同一服务器,通过RDP编码。谢谢萨阿德。

this worked for my case, where I just want to run a simple js file. But the nodemon cli command was automated to nodemon.cmd .\yourfileName.js

这对我的情况很有效,我只想运行一个简单的js文件。但是nodemon cli命令被自动设置为nodemon.cmd.YourfileName.js

Global nodemon install didn't work for me but this one did :)

全球节点安装不适用于我,但这个适用:)

This one saved me too

这一次也救了我

In your link are the instructions for linux, do you have something for windows?

在你的链接中有Linux的说明,你有Windows的东西吗?

Thanks... this worked for me, I could run nodemon from windows cmd but it was not working on visual studio code terminal nor powershell, but after execute you solution it works on all 3 consoles (I think VSC terminal is a powershell console because it has "PS" at start of prompt line).

谢谢..。这对我来说很管用,我可以从Windows cmd运行nodemon,但它不能在Visual Studio代码终端或PowerShell上运行,但在执行您的解决方案后,它可以在所有3个控制台上运行(我认为VSC终端是PowerShell控制台,因为它在提示行的开头有“ps”)。

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

这并不能提供问题的答案。一旦你有了足够的声誉,你就可以对任何帖子发表评论;相反,提供不需要发问者澄清的答案。-摘自评论

This is a repeat of accepted answer in 90% of content: stackoverflow.com/a/40359656/7264739

这是90%内容中公认答案的重复:Stackoverflow.com/a/40359656/7264739

Welcome to Stack Overflow. When answering an older question with existing answers it is useful to explain what new information your answer brings. This answer doesn't appear to be showing a global install, it references a specific version (so it won't age well), and it doesn't answer the question that was asked in that it doesn't show how to enable the command.

欢迎来到Stack Overflow。在用现有答案回答较旧的问题时,解释您的答案带来了哪些新信息是很有用的。这个答案似乎没有显示全局安装,它引用了一个特定的版本(因此它不会很陈旧),它没有回答被问到的问题,因为它没有显示如何启用命令。

It is not working this way... thats the issue...

不是这样的.这就是问题所在。

Can you share the error, so I can re-create your issue and help you better ?

你能分享这个错误吗,这样我就可以重新创建你的问题,并帮助你做得更好?

my issue is resolve with "npx nodemon app.js" BUT why?

我的问题是用“npx nodemon app.js”解决的,但是为什么呢?

that's great, the query is resolved.

太好了,查询已经解决了。

Does not answer the question. node and nodemon have different purposes.

没有回答这个问题。节点和节点有不同的用途。

it different thing. For nodemon it should be "server": "nodemon app.js"

这是不同的事情。对于nodemon,它应该是“服务器”:“nodemon app.js”

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com