I have a "server" folder in which I installed a npm package.json file and http server, and there is also a "webdir" folder with a web page (inside the "server" folder).
I wrote a command
"start": "cd webdir && node ../node_modules/http-server/bin/http-server && open http:localhost:8080"
The issue is that
http-server will run indefinitely, until you (manually) stop it. Only after it has stopped will the following command (
open ...) be executed, which is too late.
A solution is to start the HTTP server in the background. Because this may cause problems (the HTTP server will keep running even after
npm exits), you have to also wait for its exit using the shell's
"start": "cd webdir && node ../node_modules/http-server/bin/http-server & open http://localhost:8080 ; wait"
Notice that between
open is a single ampersand. This backgrounds the
Depending on how fast the server starts, you may still get an error page in the browser (when the browser requests the page before the server has fully started). To work around this, you need to add a
sleep 3; (meaning "sleep 3 seconds") between the single ampersand and
cd webdir && node ../node_modules/http-server/bin/http-server & sleep 3; open http:localhost:8080 ; wait