10

I have a simple batch file so I can easily execute npm commands without typing them every time. But when I try it with npm run build, the window closes after the script finishes.
I cannot see the results from npm run build.
I tried PAUSE or cmd /k but the cmd console window keeps closing.

create-build.bat:

cd myfolder
npm run build

npm on executed by cmd.exe is a batch file with file name npm.cmd.
See from the npm GitHub repository the directory bin with the file npm.cmd.

5
  • Are you launching your script from a short-cut, clicking on it in File Exploder or running it from the console command line? Commented Dec 28, 2017 at 18:01
  • im launching the .bat from a short-cut Commented Dec 28, 2017 at 18:02
  • See Keep CMD open after BAT file executes. Please review How to Ask, you really didn't do your homework before posting this question. Commented Dec 28, 2017 at 18:05
  • Possible duplicate of Keep CMD open after BAT file executes Commented Dec 28, 2017 at 18:05
  • i already tried pause or cmd /k Commented Dec 28, 2017 at 18:08

4 Answers 4

14

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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

Comments

0
call D:\soft\nodejs\npm.cmd run build

Comments

0

There should not be used a batch file with file extension .bat or .cmd stored in the user's desktop folder for this task. There should be used a shortcut file with file extension .lnk as that makes it possible to control all parameters on Windows shell (explorer.exe) calling the Windows kernel library function CreateProcess with a filled out STARTUPINFO structure according to the data configured in properties of the shortcut file.

The shortcut file is created as follows:

  1. Browse in Windows File Explorer to the directory System32 in the Windows directory containing cmd.exe which is usually C:\Windows\System32.
  2. Click with secondary (right) pointing device (mouse) button on the file cmd.exe to open the context menu, click on Windows 11 on the context menu item Show more options, and click in the submenu Send to on the menu item Desktop (create shortcut).
  3. Switch to the Windows desktop which has now an additional shortcut file with name cmd.exe - Shortcut.lnk whereby the file extension .lnk is not visible, just the file name cmd.exe without or with - Shortcut appended depending on version of Windows and a registry setting determining how shortcut files are named by default.
  4. Click with secondary (right) pointing device (mouse) button on cmd.exe shortcut, click on Windows 11 on the context menu item Show more options, and click in opened context menu on the menu item Rename to change the file name, for example, to NPM Build resulting in shortcut file being renamed to NPM Build.lnk.
  5. Click with secondary (right) pointing device (mouse) button on NPM Build and click in the opened context menu on the menu item Properties.
  6. There is opened the Properties dialog window with by default selected tab Shortcut. On this tab must be modified first the property Target on which to append a space character and the string /D /K npm.cmd run build. Well, even better would be appending after a space the string /D /K "C:\Full Path\npm.cmd" run build so that cmd.exe does not need to search for the file npm.cmd with using the environment variables PATH and PATHEXT.
  7. There must be next modified the property Start in where the full path of the directory must be entered which should be the current directory on starting cmd.exe for processing the batch file npn.cmd to run node.exe for the build. The Start in directory path should be defined always without double quotes even on containing a space or one of these characters &()[]{}^=;!'+,`~ as this string is not processed by the started cmd.exe but passed directly by explorer.exe to the kernel function CreateProcess with the function parameter lpCurrentDirectory.

There can also be modified other properties of the shortcut file. There can be defined a Shortcut key like Ctrl + Alt + B to be able to execute the build in the configured directory by pressing this combination of keys independent on which application has currently the input focus. There can be added a Comment like npm build run in C:\myfolder displayed as tooltip when the pointing device pointer is positioned over the shortcut on the Windows desktop. A nice icon can be selected for this shortcut and a different font or font size can be set on tab Font. It is also possible to define on the tab Layout the width (number of characters) and height (number of lines) of the console window opened on using this shortcut. The screen text color and the screen background color can be defined on the tab Colors. And much more is customizable, which is the great benefit of using a shortcut file instead of a batch file.

The last step is clicking on the button OK to save the changed shortcut properties.

The shortcut file can be kept on Windows desktop but could be moved or copied also into the Windows start menu or pinned on the Windows taskbar as it can be done with any shortcut file.

The execution of the npm run build can be simplified with the use of a shortcut file, especially on having a shortcut key defined too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.