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:
- Browse in Windows File Explorer to the directory
System32 in the Windows directory containing cmd.exe which is usually C:\Windows\System32.
- 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).
- 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.
- 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.
- Click with secondary (right) pointing device (mouse) button on
NPM Build and click in the opened context menu on the menu item Properties.
- 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.
- 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.