What is the reason for nodemon working in cmd but not in a batch file?

♀尐吖头ヾ 提交于 2020-01-06 05:37:27

问题


I am in the process of making a discord bot. All of the code that I have written for the bot works except for the batch file that is supposed to run it. Originally I was just using the node command and when I opened cmd, navigated to the folder, and typed it manually it worked fine, but when I put that same code into a batch file it gave me this error:

'node' is not recognized as an internal or external command, operable program or batch file.

This is all the code for that batch file:

@echo off
node bot.js
pause

The node command was in my path so I'm not sure why it wasn't working, but in another post, someone recommended that instead of typing just node to type the full file path, so I tried this and it worked.

Here is the new working code:

@echo off
"C:\Program Files\nodejs\node.exe" bot.js
pause

Then I installed nodemon. Again this works in the cmd when I navigated to the folder and typed it manually, but when I try to do it in the batch file it does not work. Instead of giving me the error it had been before the window just instantly closes. Here is that code:

@echo off
nodemon bot.js
pause

Since I have the pause command at the end of the code it should stop there if I get an error, but it is closing before it gets there for some reason. The nodemon command is in my path and I have also tried replacing nodemon with the file path, C:\Users\tdkni\AppData\Roaming\npm\nodemon.cmd, like I was recommended in the previous post. Neither of these solutions worked, and I think that is because there is some other problem besides the nodemon command not being detected. I don't see any error message since it is closing instantly so I don't know exactly what is wrong.

The registration of the file extensions .bat and .cmd is as follows according to an advice in a comment deleted in the meantime.


回答1:


I'd like to thank all of you for trying to help me with this problem I was having. You have all been very helpful, and while it may not have fixed my problem it did help me understand how all of this works. I just tried to start the bot using the batch file I made and it suddenly worked. I don't know why because I tried to start it the same way I had been the entire time, but as far as I could find, no one else was having this issue anyways so it probably won't matter much that I don't know what fixed it. Again, thank you to everyone that helped me with this.




回答2:


Well, it is pretty clear why node.exe was not found by cmd.exe in directory C:\Program Files\nodejs.

Local Path being system and user Path concatenated contains "C:\Program Files\nodejs;" instead of just C:\Program Files\nodejs.

Folder paths in Path should be never enclosed in double quotes with one exception: The folder path itself contains one or more ;. In this case the folder path with ; must be enclosed in double quotes to get the semicolon(s) in folder path not interpreted as separator between the folder paths. That is general CSV syntax as described on Wikipedia article comma-separated values which is used by Windows for the folder paths in Path with using semicolon as separator.

For that reason cmd.exe searches in a folder with name C:\Program Files\nodejs; for node.* with a file extension listed semicolon separated in environment variable PATHEXT. But there is no folder C:\Program Files\nodejs; because the folder is C:\Program Files\nodejs without the semicolon at end.

And also PATHEXT is defined wrong as it contains at end the folder path C:\Program Files\nodejs although it should contain only file extensions separated by a semicolon.

Other small mistakes:

  1. The first 4 folder paths in system PATH should be always:

    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0
    

    Some installers add folder paths at beginning of folder paths list instead of appending them at end as it can be seen here. The Intel64 compiler and the Oracle Java folder paths should be moved in system PATH after PowerShell folder path.

  2. Folder paths can but should not end with a backslash. All backslashes at end of a folder path should be removed from system and user PATH. Microsoft added since Windows Vista the PowerShell path with a trailing backslash for some unknown reason. But it is safe and recommended to nevertheless remove the backslash after WindowsPowerShell\v1.0.

  3. System and user Path (if latter is existing at all) and also PATHEXT should not end with a semicolon. There should be no ; after last folder path respectively last file extension as this means according to CSV specification that there is one more value (folder path, file extension) which is an empty value.

I recommend to define system Path with following value respectively folder paths:

%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0;%SystemRoot%\System32\OpenSSH;%ProgramFiles(x86)%\Common Files\Intel\Shared Libraries\redist\intel64\compiler;%ProgramFiles(x86)%\Common Files\Oracle\Java\javapath;%ProgramFiles%\nodejs;%ProgramFiles(x86)%\Windows Kits\8.1\Windows Performance Toolkit

I recommend to define user Path with following value respectively folder paths:

%LocalAppData%\Microsoft\WindowsApps;%AppData%\npm

Those two folder paths are user account related and should be added for that reason to user and not system Path.

I recommend to fix system environment variable PATHEXT to:

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

For more details about usage of Path and PATHEXT see the answer on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? Please take also a look on Wikipedia article about Windows Environment Variables.




回答3:


Give this a try for us please, if it works, I will explain:

@echo off
cd /d "C:\Users\tdkni\AppData\Roaming\npm"
echo Testing Script > OUTPUT.log
nodemon.cmd bot.js >> OUTPUT.log
pause


来源:https://stackoverflow.com/questions/52041639/what-is-the-reason-for-nodemon-working-in-cmd-but-not-in-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!