How to run React Boilerplate with forever - javascript

I'm going to run react-boilerplate application forever in the server.
I found forever and I'm not sure how I pass parameters to forever. The command to run server is like following:
PORT=80 npm run start:production
Seems like forever start PORT=80 npm run start:production doesn't help me.

One thing is that PORT=80 part is setting the env variable, this kind of command should be in front of other commands. The other thing is that to run npm scripts with forever, you need to use different syntax, so PORT=80 forever start -c "npm run start:production" /path/to/app/dir/.
If you're running forever form the project folder, the path should be ./

Or you can run a react application with pm2 or with nohup
1) install pm2 globally
npm install pm2 -g
2) navigate to the project folder and execute, space is required after --
pm2 start npm -- start
3) to see running instances
pm2 ps
4) to see the other options
pm2 --help
To run with nohup
1) navigate to the project folder
nohup bash -c 'npm start' &

pm2 is superb production process manager for Node. In addition to starting and daemonizing any application, it has a built in load balancer.
Install pm2:
npm install pm2 -g
To add start and add deamon to your app, navigate to the app folder and:
pm2 start app.js
To make pm2 autoboot on server restart:
$ pm2 startup
Then copy and paste the code generated.

For this you will need:
Install forever usingnpm install -g forever
Run the forever command PORT=<YOUR PORT> forever start -c "<command>" ./
Your command can be for example npm start and npm run dev.
Use ./ only if you are in the project folder.
Port means your port number, usually 80 or 443.

Related

How to run Typescript files on Server in background?

I have a NodeJS application which has only typescript files in it, no js files. I want to run in on my server in the background.
How can I archive that?
I tried using the npm package called forever but it only works with js files as it doesn't understand the typescript code.
You could use forever in combination with ts-node.
Here is a link to the npm package ts-node
I would suggest using the following command:
forever start -v -c ts-node app.ts
Where -v/--verbose is for turning on the verbose messages from Forever.
And -c is the COMMAND to execute which is default is node
This question is so old and forever now discourages to use it.
For new installations we encourage you to use pm2 or nodemon
Here is a quick tutorial on how to run your Typescript project as a background process.
Install PM2 globally:
npm install pm2 -g
Build your sources with Typescript default config:
tsc
You will have a new directory dist that contains your js files.
pm2 start dist/app.js
Bonus: you can monitor your app with the following command.
pm2 monit
first use
npm install -g ts-node
then use
forever start -v -c ts-node app.ts
it shuld start now
The two production quality recommendations I would make are:
Turn it into a docker container
Write a systemd service
Those are by far the best options. If for some reason this doesn't work:
pm2
supervisord

Error while running reactjs app under pm2

I have reactjs project that need to be run on the linux server. Whenever I run the app on server using "sudo npm run lin_stage_server" it seems to be working fine on 80 port.
when I tried to run app through pm2 with command "sudo pm2 start npm -- lin_stage_server" its not working for me.
In addition to the above command i also tried
"sudo pm2 start npm -- BRANCH_ENV=stage NODE_ENV=production PORT=80 node appserverstart.js
"
Please help !
----- Error details ------
Error details
----- Package.json -----
Package.json
----- appserverstart.js -----
appserverstart.js
-------Additional info-------
server: CentOS Linux version 7 core,
npm version: 6.4.1,
pm2 version: 3.5.1,
node version: 8.11.1,
You are not passing run to npm:
pm2 start npm -- run lin_stage_server
pm2 start npm -- lin_stage_server is effectively npm lin_stage_server.
pm2 start npm -- run lin_stage_server is effectively npm run lin_stage_server

node.js as localhost and use gulp to watch changes

I cloned a repo here https://github.com/willianjusten/bootstrap-boilerplate and do the following step.
git clone git://github.com/willianjusten/bootstrap-boilerplate.git new_project
cd bootstrap-boilerplate
npm install
gulp
The gulp does the work. But is the server started? In gulp js file I'm seeing the author use livereload, how do I start my development with that?
You can use nodemon (https://github.com/remy/nodemon). If any file is changed, nodemon will re-start automatically.
Sample usage
install
npm install -g nodemon
start node
nodemon server.js

gulp watch seems to freeze in Docker

I'm trying to "dockerize" our development environment. We have a gulp build system that watches changes to our js/sass/jade files. This is all setup to work just fine outside of docker.
I've created a docker container and I mount my code base into it (using a volume). All the precursor npm installs and bower installs finish successfully. My last step runs gulp and it runs properly and builds but then does not pick up any subsequent changes to any of our js/sass/jade files.
I'm running the build system with the following command:
docker run -it -v $(pwd):/code/ client gulp reset
Does anyone have a similar setup in their development environment? What did you do to get your gulp watch to work and display the building?
EDIT: I guess I could do the gulp build/watch outside of docker and only mount the generated files but I'd rather contain that all inside of docker so that the host machine doesn't need to worry about any dependencies to build/run our app
EDIT2: Here are my dockerfile and docker-compose.yml
#Dockerfile
FROM node:0.12.5
RUN mkdir /code
WORKDIR /code
RUN mkdir client
WORKDIR client
RUN mkdir .tmp
ADD ./client/package.json /code/client/package.json
ADD ./client/bower.json /code/client/bower.json
RUN npm install gulp -g
RUN npm install bower -g
RUN npm install
RUN npm rebuild node-sass
RUN bower --allow-root install
CMD gulp reset
and
client:
build: .
volumes:
- .:/code
I've never been able to get any inotify-based file watcher to ever work over with virtual-box guest additions, and based on this ticket it's unlikely to be available anytime soon. My preferred approach is the following:
Assuming my local source code is in /code
Run my watcher locally on /code
When a change is detected, rsync local /code to remote /code (mounted as a container-only volume) in the container
Example rsync:
docker run --rm --volumes-from sourcecode my/image \
rsync \
--delete \
--recursive \
--safe-links \
--exclude .git --exclude node_modules \
/local/repo/ /container/repo
This avoids lots of issues and allows you to get granular with what you want your container environment to see.

Running node-inspector alongside nodemon?

I'm currently using node along with nodemon. Then I got to thinking it might be sometimes nice to use an inspector with node so have started using node-inspector
However, is it possible to run both at the same time?
Normally to run nodemon I would use:
nodemon server.js
//and similarly
node-debug server.js
I have also tried:
nodemon --debug http.js
But sadly this didn't work either.
But both together!?
If you want to run them as one command this works for me: node-inspector & nodemon --debug app.js (replacing app.js with the name of your script). If things get all mucked up you will occasionally have to kill node-inspector manually, but running the command this way gives you the option of running rs to restart nodemon manually if needed. HTH
You would start your server with nodemon --debug server.js and then you'll need to run node-inspector in a separate terminal window unless you push nodemon to the background.
For those that want an OS-independent solution and no hacks for windows, etc.
You can use npm-run-all which is a CLI tool that allows running multiple npm scripts in parallel or sequentially. So you'd set your package.json as so:
"scripts": {
"start": "npm-run-all --parallel lint start:debug start:server",
"lint": "eslint . --ext .js",
"start:debug": "node-debug server.js",
"start:server": "nodemon server.js"
}
And then from CLI, you do: npm start
Caveat: from my experience running nodemon and node-debug together leads to weird node-inspector behaviors sometimes. So i've since opted to remove nodemon from my scripts when debugging and relying on node-inspectors save-live-edit feature to change files on the fly.
I could not get nodemon to play nice with node-inspector. After one change it would restart but after that no more. Maybe it is because I am using docker containers.
The easiest way to reload the application is to let node-inspector do it (I know this is not an answer to having both run but it worked for me).
Start your application in the following way:
node-inspector --save-live-edit & \
node --debug /app/server.js
As I'm running on Linux I wrote a bash script based from rpaskett's answer so that you don't need to remember that awkward command every time.
However I noticed in a comment you're running Windows. Here are some options you have:
You could convert the bash script to Windows batch and save it as C:\Windows\System32\node-DEV.bat. I did it and it works on my Windows PC:
#echo off
echo Starting DEV environment for %1
start node-inspector
nodemon --debug %1
Then you should be able to run node-DEV server.js.
Another option; you could run something like nodedev which was written in Node.js thus platform independent, although it looks like it hasn't been updated in a while.
Or you could even run the bash script within a Cygwin environment if you had one handy.
A hacky fix for Windows users running a bash shell:
First, add node-inspector to your Path.
(You can find where npm is installing packages with npm list -g)
Then use this command in bash, or add it to your npm scripts:
START /B node-inspector && nodemon --debug server.js
START /B being the windows command to run in the background.
You must be install node-inspector and nodemon using:
npm install -g nodemon
npm install -g node-inspector
To run in Windows, make a new .bat file and add the folowing lines:
#echo off
echo Starting developer enviroment of the file %1
start nodemon --debug-brk %1
node-debug %1
And run:
node_desarrollo.bat "name of the file to run.js"
If ran with a error:
Error: listen EADDRINUSE :::5858
at Object.exports._errnoException (util.js:855:11)
at exports._exceptionWithHostPort (util.js:878:20)
at Agent.Server._listen2 (net.js:1237:14)
at listen (net.js:1273:10)
at Agent.Server.listen (net.js:1369:5)
at Object.start (_debug_agent.js:21:9)
at startup (node.js:72:9)
at node.js:980:3
Its normal because the node-inspector need to open that port to connect but because the nodemon --debug-brk %1 was opened the 5858 port its cannot open and show the EADDRINUSE :::5858 error, note that the flag --debug-brk of nodemon it's necessary to make a breakpoint on the first line. Try modifying the file.js after run the .bat and look the changes reflected on the debugger. This debugger reboots and show the changes done in the file.js. Happy coding JS!!!
{
"scripts": {
"dev": "npx nodemon --exec \"node --inspect --debug-port=0.0.0.0 src/index.js\""
}
}

Categories

Resources