I’m trying to run a React server locally, I don´t need it to be public (as in open to the Internet public), just accessible in my LAN.
The first step was to bring the server available for other devices in the LAN when you execute $ npm start
, and that’s already done.
The next step would be to run $ npm start
automatically. This is where I’m finding dificulties.
A .bat file in the shell:startup folder should do the trick (I’m using Windows BTW).
I’m running into problems when the .bat file finishes executing, because it immediately closes the git-bash where React would be running, thus killing the process and shutting down the server.
For tests, I’m running the commands in the cmd line.
These are the variations of the code in the .bat file:
First I tried this:
cd C:theroutetomyreact-project
start "" "C:Program FilesGitbinbash.exe" -c "cd /C/the/route/to/my/react-project && npm start"
And then this:
@echo off
start "" "C:theroutetoGitgit-bash.exe" --login -i -c "cd /C/the/route/to/my/react-project && npm start"
I’ve tried appending && exec bash"
to the 2nd line to keep the git bash open but it didn’t change any behaviour.
Right now I’m pretty much out of ideas about what to do…
Is this even possible?
4
Answers
To keep the command window open after running your React server with a batch file, use
cmd /k
followed by your script. This keeps the window open even after the script executes, allowing your server to keep running.There should be never a
.bat
or.cmd
batch file stored in the directory%APPDATA%MicrosoftWindowsStart MenuProgramsStartup
. This directory should contain only shortcut files with file extension.lnk
. The Windows File Explorer does not display the file extension.lnk
by default.The shortcut file for the purpose of starting React server can be created as follows:
C:WindowsSystem32cmd.exe
.cmd.exe - Shortcut
.React Server
. The file extension.lnk
is kept.React Server
and left click on context menu item Properties./D /K "%USERPROFILE%ReactServer.cmd"
.C:theroutetomyreact-project
. (No surrounding"
!)Starts Command Processor for execution of React Server and keeps running
.cmd.exe
while with a batch file the default settings are used as stored under registry keyHKEY_CURRENT_USERConsole
. Then click on button OK.React Server
from desktop, restore Windows File Explorer, browse to directory%APPDATA%MicrosoftWindowsStart MenuProgramsStartup
and paste the shortcut file there.The batch file must be created in the directory
%USERPROFILE%
with the file nameReactServer.cmd
as that was specified on the command line of property Target in the shortcut file.The file extension
.bat
is for batch files interpreted byCOMMAND.COM
of MS-DOS, Windows 95/98/ME. The file extension.cmd
is for batch files interpreted by the Windows Command Processorcmd.exe
since Windows NT. Batch files written for execution on Windows XP/Vista/7/8/8.1/10/11 should have the file extension.cmd
and not anymore.bat
.The shortcut file defines with Start in already the current working directory for
cmd.exe
and all executables started bycmd.exe
during processing of the batch file. The batch file does not need anycd
command. But there can be used below@echo off
nevertheless:That makes it possible to run this batch file without the shortcut file like from within a command prompt window.
I do not know it for sure, but Git Bash should not be needed at all. It should be enough using as next command line in the batch file:
The entire batch file
%USERPROFILE%ReactServer.cmd
would be:A batch file is not needed at all if there is nothing more added like
title React Server
and other command lines. The property Target of the shortcut file could be also:That results in same execution behavior without using a batch file and is therefore faster executed with less file system accesses.
PS: A programmer information: A shortcut file stores the properties passed by
explorer.exe
to the Windows kernel library function CreateProcess with its function parameters and the STARTUPINFO structure. Property Target is the string passed with function parameterlpCommandLine
toCreateProcess
. Property Start in is the string passed with function parameterlpCurrentDirectory
toCreateProcess
. The property Run defines the integer value forwShowWindow
in STARTUPINFO structure withdwFlags
with flagSTARTF_USESHOWWINDOW
set. The integer value ofwShowWindow
is passed further to the called ShowWindow function with parameternCmdShow
. And so on for all other properties on the other shortcut property tabs.you can try the following approach:
Create a
.bat
file** with the following content:Place the
.bat
file in the Startup folder:Win + R
, typeshell:startup
, and press Enter..bat
file into the Startup folder.The key part here is the
exec bash
at the end of the command, which should keep the Git Bash window open after starting the server.Alternatively, you can use a Task Scheduler to run the script at login:
Open Task Scheduler:
Win + R
, typetaskschd.msc
, and press Enter.Create a new task:
General tab:
Triggers tab:
Actions tab:
.bat
file.Conditions tab:
Settings tab:
Easy way; install node-windows package from npm