Reputation: 55
I am trying to make a .bat file for running two separate apps locally, a React app and the Strapi service it depends on. My OS is Windows 10.
So far I came up with this:
echo Start React started
C:\Windows\System32\cmd.exe /k "C:\Program Files\nodejs\nodevars.bat"
cd "C:\Websites\React\react-template-app"
call npm start
It does launch a Node JS command prompt (line 2) and changes directory.
But it just will not run the npm start
command. I tried several syntax variants.
When I type npm start
in the command line opened by the script, it runs fine.
Upvotes: 0
Views: 1504
Reputation: 49097
A batch file is processed by Windows command processor cmd.exe
. The second command line starts one more cmd.exe
to process the batch file nodevars.bat
which defines multiple environment variables in context (memory) of this second command process. So the batch file npm.cmd
being processed by first started cmd.exe
cannot make use of the environment variables defined in environment of second command process. See also the long answer on What is the reason for "X" is not recognized as an internal or external command, operable program or batch file"?
I recommend not using a batch file for this task, but a shortcut file with file extension .lnk
.
There is in Windows Start menu by default a shortcut to open a Command Prompt (file name on English Windows). Click with secondary (right) pointing device (mouse) button on this shortcut file and copy it. Then paste the shortcut file either on your Desktop or in a folder of the Start menu. Next click with secondary pointing device button on the pasted shortcut file and use Rename to change the file name, for example, to Start NPM
.
Click with secondary pointing device button once more on the shortcut file for starting NPM and click in opened context menu on last menu item Properties.
There is opened a dialog window with several tabs. The displayed tab contains already the property Target which contains cmd.exe
with full path. Append after cmd.exe
a space and the following arguments:
/K "%ProgramFiles%\nodejs\nodevars.bat" & npm.cmd start
Next there is the property Start in which defines the current working directory for the executable started by Windows Explorer and defined with the property Target. Modify this property to:
C:\Websites\React\react-template-app
There is also the property Shortcut. By clicking into edit field and pressing a key combination like Ctrl+Alt+N, this key combination can be pressed later at any time with any window having currently the input focus to let Windows Explorer execute Target to start NPM with the configured properties.
There can be more properties configured to whatever is preferred like the icon, the font and font size for the console window, the background and text colors, the number of rows and columns (window height and width), etc.
Click finally on button OK and the shortcut file to start NPM is ready for usage. The shortcut file can be pinned on Windows task bar, but with having a Shortcut key combination defined for this shortcut file, it does not really matter in which folder the shortcut file is stored for execution.
Upvotes: 1
Reputation: 80033
nodevars
which I presume is setting variables in the environment required for npm
are being set in the context of the new window, so npm
runs there with the variables set. The variables are not set in the context of the main batch, so it fails there.
Try simply call "C:\Program Files\nodejs\nodevars.bat"
instead or ...cmd.exe...
Upvotes: 0