Error when starting pm2 getting module_conf.json not existing - node.js

When I start pm2, I get this
Error: ENOENT: no such file or directory, open 'C:\WINDOWS\system32\.pm2\module_conf.json'
how can I fix this?

1 . Add user environment variable as below:
PM2_HOME=%USERPROFILE%\.pm2
2 . Restart command line
cmd
3 . Then kill pm2 daemon once
pm2 kill
4 . Now run pm2 as usual (An example is below)
pm2 start ./bin/www

Related

Start multiple processes via npm scripts using PM2

I want to start multiple processes by pm2.
I found how to start one process for npm run start:
pm2 start npm -- start
But when I've tried make something like pm2 start npm -- event for npm run event it doesn't start a new process but restarts the first one.
How can I start multiple process by npm and pm2?
Use the --name flag to give each processes a unique name for PM2 to identify them by.
For example:
pm2 start --name=start npm -- start
pm2 start --name=event npm -- run event
Further operations should use the name you have given each process. For example to stop them:
pm2 stop start
pm2 stop event

Cannot run SailsJS app with forever

I am installing an SailsJS app on an Amazon Ubuntu instance.
If I run the app with sails lift everything works and the program run successfully. Now I try to run it through forever for obvious reasons. As specified by the SailsJS documentation I run forever on the app.js file located at the root of my project : forever start app.js
But that time the app fails :
forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] XXXX /usr/bin/nodejs app.js 15348 15350 /home/ubuntu/.forever/XXXX.log STOPPED
Logfile contains only :
error: Forever detected script exited with code: 0
Any idea why sails lift would work but not with forever ?
Sometimes there are permissions errors. Try to verify forver's permissions files in .forever directory or maybe run with sudo.
I run forever start app.js with sudo and I did't have any problems.
After that sudo forever list a get:
data: [0] kBdX /usr/bin/nodejs app.js 23533 23538 /home/ubuntu/.forever/kBdX.log 0:0:34:28.312

Forever with npm start and environment variable

I used to start my application using:
DEBUG=chakka ENVIRONMENT=production npm start
How can i start it using forever so i wouldn't have to do it everytime i want to test the application? Thanks!
First you need to know what the application's main script file is. Open up your package.json file and find out what the start script is. If you're using Express it might be app.js. So we'll assume app.js for this example, replace with whatever your file is.
To start the application:
DEBUG=chakka ENVIRONMENT=production forever start app.js
to restart the application after you've made changes:
forever restart app.js

nodejs + nodemon + forever give me an error

I just installed forever globally (-g). Before that I used to run with
$ npm start
Now after installed forever, I tried to lunch the node app
$ NODE_ENV=development forever nodemon server.js
but I receive this error
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
error: Cannot start forever
error: script /path/to/app/nodemon does not exist.
the same also with
$ NODE_ENV=development forever nodemon server.js
any idea?
The error you received in your output:
error: script /path/to/app/nodemon does not exist.
It appears that forever is looking for nodemon in the current working directory, and can't find it because it doesn't exist there. Try providing the absolute path when starting nodemon, which can be found with which nodemon.
forever start /usr/local/bin/nodemon server.js
Note that the start flag is what puts the application in daemon mode.
Try this
NODE_ENV=development forever start -c nodemon server.js
The -c is for execute commands, forever send you that error because it's looking for a app called nodeamon, but your app is server.js

Forever + Nodemon running together

Is there any way to have both of this packages running together?
So basically I want to have best from both worlds. Running server automatically (and restarting when there is an error) and also automatic updates when there is .js file change happening.
You should run something like this
forever start -c nodemon app.coffee
Toxa was on the right track, the issue that cfogelberg raised is valid, but to avoid that issue you can do the following:
forever -c "nodemon --exitcrash" app.js
this makes sure nodemon actually exits (rather than giving you the "app crashed" message) and then forever picks it up again.
In forever --help this -c specifies a command to run otherwise it defaults node. Without -c results in the error that is mention in the comments to this answer.
There is an entry about it in the nodemon FAQ:
If you're using nodemon with
forever (perhaps in a
production environment), you can combine the two together. This way if
the script crashes, forever restarts the script, and if there are file
changes, nodemon restarts your script. For more detail, see issue
30.
To achieve this you need to add the following on the call to
forever:
Use forever's -c nodemon option to tell forever to run nodemon instead of node.
Include the nodemon --exitcrash flag to ensure nodemon exits if the script crashes (or exits unexpectedly).
Tell forever to use SIGTERM instead of SIGKILL when requesting nodemon to stop. This ensures that nodemon can stop the watched node
process cleanly.
Optionally add the --uid parameter, adding a unique name for your process. In the example, the uid is set to foo.
bash forever start --uid foo --killSignal=SIGTERM -c nodemon
--exitcrash server.js
To test this, you can kill the server.js process and forever will
restart it. If you touch server.js nodemon will restart it.
To stop the process monitored by forever and nodemon, simply call the
following, using the uid we assigned above (foo):
bash forever stop foo
This will stop both nodemon and the node process it was monitoring.
Note that I would not recommend using nodemon in a production
environment - but that's because I wouldn't want it restart without my
explicit instruction.
I have not found a way of getting both packages running together. I tried to do #toxa's technique, but when my node.js app threw an exception nodemon would not automatically restart it, instead outputting an error message to the forever log:
nodemon] app crashed - waiting for file changes before starting...
However, forever has a -w option and the following command is effectively the same as if I'm running nodemon and forever together:
forever start -w my-app.js
The downside of forever -w versus nodemon: forever does not have a --delay option, so my server gets restarted once for each file that is changed.
I prefer a combo of what Toxa and Jubair suggest.
forever start -c nodemon app.coffee --exitcrash
If you need to pass arguments:
forever start -c "nodemon --harmony" app.js --exitcrash
I'm using forever-service . . .
This is what worked for me. It does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):
$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"
Then I can run:
$ service raml start|stop|restart|status
I can also have the service start on server reboot with the chkconfig utility:
$ chkconfig --add raml
$ chkconfig raml on
when using in the package.json use single quotes to make nodemon --existcrash as a single argument.
"start": "forever -c 'nodemon --exitcrash' server.js"
Output:
app_1 | [nodemon] app crashed
app_1 | error: Forever detected script exited with code: 1
app_1 | error: Script restart attempt #1
app_1 | [nodemon] 1.19.4
app_1 | [nodemon] to restart at any time, enterrs
app_1 | [nodemon] watching dir(s): *.*
app_1 | [nodemon] watching extensions: js,mjs,json
app_1 | [nodemon] startingnode /app/server.js`
app_1 | app is running on port 3000
`
Use like this: "start": "firever -c \"nodemon --exitcrash\" <main>.js"

Resources