skip to Main Content

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


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

    Login or Signup to reply.
  2. 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:

    1. Browse in Windows File Explorer to the file C:WindowsSystem32cmd.exe.
    2. Right click on this file for opening the context menu and on Windows 11 left click on Show more options and click in submenu Send to on Desktop (create shortcut).
    3. Minimize Windows File Explorer to see the desktop with new shortcut cmd.exe - Shortcut.
    4. Right click on the shortcut file and on Windows 11 left click again on Show more options and left click on Rename and rename the shortcut file to React Server. The file extension .lnk is kept.
    5. Right click once again on shortcut React Server and left click on context menu item Properties.
    6. Expand property Target by appending a space and /D /K "%USERPROFILE%ReactServer.cmd".
    7. Modify property Start in to C:theroutetomyreact-project. (No surrounding "!)
    8. Modify property Comment to something like Starts Command Processor for execution of React Server and keeps running.
    9. Modify other properties on other tabs. That is the big advantage of a shortcut file in comparison to a batch file. The number of rows and columns, the colors, the font, and much more can be configured for this execution of cmd.exe while with a batch file the default settings are used as stored under registry key HKEY_CURRENT_USERConsole. Then click on button OK.
    10. Cut the shortcut file 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 name ReactServer.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 by COMMAND.COM of MS-DOS, Windows 95/98/ME. The file extension .cmd is for batch files interpreted by the Windows Command Processor cmd.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 by cmd.exe during processing of the batch file. The batch file does not need any cd command. But there can be used below @echo off nevertheless:

    cd /D "C:theroutetomyreact-project"
    

    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:

    call npm.cmd start
    

    The entire batch file %USERPROFILE%ReactServer.cmd would be:

    @echo off
    cd /D "C:theroutetomyreact-project"
    call npm.cmd start
    

    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:

    C:WindowsSystem32cmd.exe /D /K npm.cmd start & title React Server
    

    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 parameter lpCommandLine to CreateProcess. Property Start in is the string passed with function parameter lpCurrentDirectory to CreateProcess. The property Run defines the integer value for wShowWindow in STARTUPINFO structure with dwFlags with flag STARTF_USESHOWWINDOW set. The integer value of wShowWindow is passed further to the called ShowWindow function with parameter nCmdShow. And so on for all other properties on the other shortcut property tabs.

    Login or Signup to reply.
  3. you can try the following approach:

    1. Create a .bat file** with the following content:

      @echo off
      start "" "C:Program FilesGitgit-bash.exe" --login -i -c "cd /C/the/route/to/my/react-project && npm start && exec bash"
      
    2. Place the .bat file in the Startup folder:

      • Press Win + R, type shell:startup, and press Enter.
      • Copy your .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:

    1. Open Task Scheduler:

      • Press Win + R, type taskschd.msc, and press Enter.
    2. Create a new task:

      • Click on Create Task in the right-hand Actions pane.
    3. General tab:

      • Name your task (e.g., "Start React Server").
      • Select Run only when the user is logged on.
    4. Triggers tab:

      • Click New.
      • Set the trigger to At log on.
    5. Actions tab:

      • Click New.
      • Set the action to Start a program.
      • In the Program/script field, enter the path to your .bat file.
    6. Conditions tab:

      • Uncheck Start the task only if the computer is on AC power if you want it to run on battery as well.
    7. Settings tab:

      • Ensure Allow task to be run on demand is checked.
    Login or Signup to reply.
  4. Easy way; install node-windows package from npm

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search