0

I am trying to use a batch file to quickly start my web app locally because there are many commands to run. I am using the following:

start "" /d "%~dp0client" npx webpack --watch
start "" /d "%~dp0client" npx live-server --entry-file=index.html
start "" /d "%~dp0server" npx nodemon index.js

It is correctly opening 3 windows. The first 2 are running correctly. But in the npx nodemon index.js window, it errors and says "'webpack' is not recognized as an...". Even though the webpack command executes correctly in its window. So it seems like there is some residual stuff from the webpack command is somehow interfering with the nodemon command.

It doesn't show what commands each one actually ran, so I'm having a hard time figuring this out. Why are they interfering with each other? Did I get the syntax wrong?

1
  • webpack might not be in path? Can try adding `%USERPROFILE%\AppData\Roaming\npm` to path. Also, did you try running cmd as administrator before running this batch? Are the results different if so?
    – Narzard
    Commented Aug 15, 2022 at 17:29

2 Answers 2

1

You're running in effect all these processes in parallel.

If the webpack process needs time to initialize itself, any process that needs it will fail if launched while webpack is not ready.

To avoid this problem, add some timeout before the nodemon command.

For example, this waits for 5 seconds while discarding the output of the timeout command:

start "" /d "%~dp0client" npx webpack --watch
start "" /d "%~dp0client" npx live-server --entry-file=index.html
timeout 5 > nul
start "" /d "%~dp0server" npx nodemon index.js

Reference : timeout command

0
Command A & Command B

Execute Command A, then execute Command B (no evaluation of anything)

Command A | Command B

Execute Command A, and redirect all its output into the input of Command B

Command A && Command B

Execute Command A, evaluate the errorlevel after running and if the exit code (errorlevel) is 0, only then execute Command B

Command A || Command B

Execute Command A, evaluate the exit code of this command and if it's anything but 0, only then execute Command B

The double pipe || symbols only run the next command if the previous command failed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .