5

I have a simple .bat file so i can easily execute npm commands without typing them everytime, but when i try it with 'npm run build' the window closes after the script finishes.
I cant see the results from npm run build :/.
I tried PAUSE or cmd /k but the cmd keeps closing...

create-build.bat:

cd myfolder
npm run build

Thanks in advance!

Karl B.
  • 213
  • 4
  • 13
  • and npm is what? an executable or a batch? – Magoo Dec 28 '17 at 17:56
  • Are you launching your script from a short-cut, clicking on it in File Exploder or running it from the console command line? – jwdonahue Dec 28 '17 at 18:01
  • im launching the .bat from a short-cut – Karl B. Dec 28 '17 at 18:02
  • See [Keep CMD open after BAT file executes](https://stackoverflow.com/questions/17957076/keep-cmd-open-after-bat-file-executes?rq=1). Please review [Ask], you really didn't do your homework before posting this question. – jwdonahue Dec 28 '17 at 18:05
  • Possible duplicate of [Keep CMD open after BAT file executes](https://stackoverflow.com/questions/17957076/keep-cmd-open-after-bat-file-executes) – jwdonahue Dec 28 '17 at 18:05
  • i already tried pause or cmd /k – Karl B. Dec 28 '17 at 18:08
  • @KarlHusten `npm` is not the full file name. In real it is a batch file and not an executable. I have never installed NPM and for some unknown reason no NPM user asking for help has ever posted complete name of file with file extension. I would be really interested if the name of the batch file is `npm.bat` or `npm.cmd`. However, batch files must be __called__ from within a batch file with command `call` or the Windows command interpreter does not come back to calling batch file, see [this answer](https://stackoverflow.com/a/24725044/3074564) for details and variants on running a batch file. – Mofi Dec 29 '17 at 19:46
  • @KarlHusten Nothing can be done to prevent closing the console window if the `npm` batch file contains the command `exit` __without__ the option `/B`. In this case the command process terminates itself independent on being started with option `/K` and independent on calling hierarchy. That's the reason why I nearly never use just `exit` in batch files. It makes debugging a batch file in a command prompt window impossible when this command is executed during processing of the batch file. – Mofi Dec 29 '17 at 19:51
  • I have the same issue. So for example if my .bat file contains: – Shaedo Aug 21 '19 at 07:09

2 Answers2

6

prefix 'call' to your npm commnands: eg

call npm i -D eslint eslint-config-airbnb-base eslint-pl

This is a work around, and it's good to understand the limitations. A decent discussion can be found here: https://github.com/npm/npm/issues/2938

Shaedo
  • 231
  • 3
  • 8
  • 1
    It's still the top hit in Google so I've linked it to an answer that addresses the question two years later. This is nothing to do with the 'duplicate questions' that other people have linked this to. – Shaedo Aug 21 '19 at 07:17
1

npm evidently does an EXIT. here is the command line I use to do that.

CMD /C "C:\Program Files\nodejs\npm" i --loglevel error
RGuggisberg
  • 4,379
  • 1
  • 16
  • 23