Create-react-app: npm start / react-scripts start needs a way to daemonize

Created on 23 Nov 2016  路  15Comments  路  Source: facebook/create-react-app

running the dev server via "npm start" is nice and all, but there's no way to daemonize and/or easily detach from the console and leave the server running for longer-term dev testing.

Normally I would start a process like this:
daemon -r myprocess

or I would actually daemonize it using the "daemonize" tool.

or I would run a node server using PM2 as such:
pm2 start myserver

or I would just use this old linux trick to detach from the shell

((myprocess &)&)

but none of these tricks works with "npm start" or "react-script start". I can't detach from the shell and leave the process running. I can't daemonize. So, it's hard to do long-term testing...

proposal

Most helpful comment

thanks @bew-dev

pm2 start node_modules/react-scripts/scripts/start.js --name react-app

All 15 comments

@Tectract there is a file deep in the create-react-app node_modules that you can swap out node for pm2 start I will try and find that location for you. This is a hack but the problem is when you update the core module this will get over written in the future :(

Yeah, so the ideal solution would be a way to start the server using PM2, or with a "-r" flag that would cause it to daemonize.

@Tectract really busy right now and couldn't find it.

You find it, post it here...if I find it I'll post it in this thread next day or so. It will come in handy for others in future :)

but none of these tricks works with "npm start" or "react-script start"

Can you help us understand why?
I have very little knowledge about how processes work on Unix.

@gaearon @Tectract for our use case, we are doing beta testing so we are just running the following recommendations you mention for right now, the plan was to update the script in the modules to over ride the node execution process to pm2 start ....

Also it spawns and forks an additional child process when just running PM2 on top of your scripts which unless I turn on cluster mode there shouldn't be happening.

The other week the file was located in the react-scripts module I could of sworn but last night when I checked I couldn't find it. When I have time again I will locate that file.

@gaearon @viankakrisna do you know where the actual file is located in modules folder that executes the node process for "npm start" or "react-script start"? The other week I found it in the src and it was put in a if/else statement but cannot find it :(

The goal is to start the process in a way that, if the terminal connection gets interrupted, the process doesn't die. This is called "daemonification". Usually, tools will have a command-line option to daemonify themselves, or they can be called under the "daemon" program, or formally "daemonized" by creating an "init script", or pseudo-daemonized by wrapping them withing this (( &)&) bash shell command.

There is no easy way to daemonize tools that are started via "react-scripts start" embedded within a package.json file, which are started using "nmp start". That's the issue. So I can't just start the react server and disconnect from the webserver and forget about it, I have to either "npm run build" and serve the files with apache, which is then minified javascript and harder to troubleshoot, or I have to keep my terminal connection open to the webserver in order to keep it running.

I ended up just running "npm run build" and serving the app with apache...

@Tectract I am gonna make it my mission to find where we update it, your solution was temp same as mine. Let me know if you have issues with this implementation.

@Tectract I know I'm late to the party here but can we not just use a session manager like Screen, tmux, or byobu to manage a persistent terminal session.

If you lose terminal connection the app is "daemonized" already and you can attach to the existing session when you reconnect.

screen is not a real deamon, but sure you could use that. I just never use screen because it's impossible to remember all the commands for how to use it. Fact is though this program is built in a way such that it is impossible to deamonify 'properly', and that should be remedied probably.

There is no easy way to daemonize tools that are started via "react-scripts start" embedded within a package.json file, which are started using "nmp start". That's the issue.

The question is, why? What makes it different?

I鈥檇 like to either close this issue, or get some ideas about the solution in case you know what鈥檚 stopping us from fixing it. Thanks!

start script is called here in node_modules/react-scripts/bin/react-script.js
and is found here node_modules/react-scripts/scripts/start.js

you might be able to merge this functionality into the runDevServer method, where it instantiates WebpackDevServer, or whatever server your using.
https://github.com/Unitech/pm2/issues/2377#issuecomment-265131816

Personally i like to launch as screen daemon, you can put your own script for this in bin/www for that matter, and have it bootstrap in this way, cd to yoru app, then
screen -S myApp -d -m sh -c "while :; do npm start; done;"
then chmod a+x bin/www
then to launch it it can be just $ bin/www
To get into it, when using screen, ALWAYS use the RA switch, just screen -RA,
then ctrl c to kill

If screen -RA gets you into the wrong screen you need to exit that one, cause your not sure what your doing, so just type exit(or ctrl + a then k if you have no cursor), until your back to your command line that you recognize, otherwise keep typing exit, then when your back there, type in screen -list to see whats going on, then get into each like this screen -RA 345 where 345 being the first few numbers of the one you want to launch, then exit that one too, until screen -list doesnt show anymore, then start over. :-)

e.g.

myuser@myhost ~/sites/react-rest $ bin/www
myuser@myhost ~/sites/react-rest $ screen -list
There is a screen on:
        14476.myApp     (Detached)
1 Socket in /var/run/screen/S-myuser.
myuser@myhost ~/sites/react-rest $ screen -RA myApp

ctrl + a then d

thanks @bew-dev

pm2 start node_modules/react-scripts/scripts/start.js --name react-app

OK, seems like this is not something we need to solve on our end.
I hope this command (https://github.com/facebookincubator/create-react-app/issues/1089#issuecomment-294428373) works well for you!

(And don鈥檛 forget npm start is only meant for development.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jnachtigall picture jnachtigall  路  3Comments

wereHamster picture wereHamster  路  3Comments

rdamian3 picture rdamian3  路  3Comments

fson picture fson  路  3Comments

alleroux picture alleroux  路  3Comments