How to start Node.js service automatically on CENTOS 6.7? - node.js

I have two servers running nodejs applications. I did some setting with the first one before. After that setting, when I start the command line, if I run node command, I see the service is running.
But I do not remember what I did. So in my second server, anytime I restart command line session, when I type node I get -bash: node: command not found.
Could anyone remind me please?
NOTE: Please don't tell me this is duplicate. Search for keywords "start, node service, automatically, etc." most of them tell about the use of 'forever'. I know forever (gdi), mine is a lot more stupid question and I don't know the correct terminology just yet.

I would suggest using pm2 or forever
For pm2 do the following
Install it using
npm install pm2 -g
-g installs it globally. Then do following
pm2 start app.js --name="api"
Once that is done, you can do pm2 list to view all running services as follows
Make pm2 start at boot time
pm2 startup
This will automatically start your node.js app.
Works for my 4 apps that are in production.
Hope this helps.

Related

Weird PM2 - NodeJS Version Behaviour

I am experiencing a weird bug with NodeJS's File System module, where the remove directory function is asking for a callback, despite there being one. Having a look, it turns out this is a bug with NodeJS that was resolved in later releases
I tried to update the server's nodeJS version, except, as NVM was reporting, it was already fully updated.
Using the pm2 show APP_NAME command, I determined that PM2 was running the app in NodeJS v10.19.0, and all steps to update it were fruitless.
So how can I fix this?
Notable Details:
DigitalOcean Ubuntu 20 Droplet
PM2 Installed
NodeJS v16.15.1
NVM Installed
In order to resolve this issue, I had to kill the current in memory version of PM2, and go to the following directory as root:
~/.nvm/versions/node/v16.15.1/bin in there is a pm2 executable.
In order to get the application to run in Node v16.15.1, I had to run the following command ~/.nvm/versions/node/v16.15.1/bin/pm2 start index.js --name APP_NAME, and then and only then did it run the application properly.

How do I run a Node.js script in a jail after it boots in TrueNAS

I'm very new to server related stuff, but I'm trying to have a node.js server start automatically in a TrueNAS jail upon booting up (instead of starting the server manually through the terminal).
The most promising thing I could find is adding some kind of script to run at startup in the /etc/rc.d directory, but I couldn't find any specific information on running a node.js server from it. Sorry if that doesn't make any sense at all.
M'kay I found a solution. For anyone else who's curious there's an npm package called pm2 that does exactly what I was trying to do. Here's how I got it working:
Install pm2: npm install pm2 -g
Make pm2 run on startup: pm2 startup
If you get an error that says Faliure when trying to write startup script then enter the command: mkdir /usr/local/etc/rc.d to create the directory it's looking for, then run pm2 startup again and it should work
Then start your node.js app: pm2 start app.js
Finally run pm2 save to automatically start your app whenever the jail boots up

How can Nodejs apps always run as a Windows service?

I have windows Server 2016 cloud hosting. I broadcast my sites on IIS.
I have 2 apps running on Nodejs. I log out of windows after configuring my applications.
My IIS sites continue to run, but my nodejs applications quit when I log out or restart windows.
I have followed the steps below to fix this issue.
I installed the pm2 library. Globally.
npm install --global pm2#latest
I installed the pm2-windows-service library. Globally.
npm i pm2-windows-service -g
I installed the pm2-windows-startup library. Globally.
npm install pm2-windows-startup -g
I uninstall running services to avoid running apps repeatedly.
pm2 kill
If there are any running services, I first uninstall it.
pm2-service-uninstall
I installed the pm2-service-install library. I named it "pm2Service1". I see it started when I enter services.msc.
pm2-service-install -n pm2Service1
I start the startup service for applications to start when Windows opens.
pm2-startup install
I go to the directory where the application is installed and run the application with pm2. (If I don't enter the directory and run it, it gives an error. Can't read the sql / file.sql file.)
c:
cd C:\webSites\myService1
pm2 start app.js
I check the application with list and show, and see if it works.
pm2 list
pm2 show 0
After making sure it works.
I'm recording running applications.
pm2 save --force
When I do these procedures and log off windows, I can access the application from outside.
But when I restart windows the application does not work. I have to repeat the same steps every time it starts up.
When Windows restarts, "pm2Service1" seems to be working. But pm2 list the lists as empty.
I accomplished this on my servers by using NSSM Service. Once you install it, go to NSSM folder and type
nssm install PM2
You can find the entire tutorial here:
https://lakin-mohapatra.medium.com/register-pm2-as-a-service-in-the-windows-server-747f19e2ff2a
Best practice AFAIK is using nssm + "windows batch file" to start your app.js, for more details you can check this link.

How to use a local version of pm2 in node_modules directory to keep a server alive?

I want to keep my node server alive. Therefore I use pm2 but if I try to start my server with
pm2 start index.js
I get the message:
pm2: command not found
So, I wanted to ask how to use local pm2 in node_modules directory without installing pm2 globally.
Do I have to register pm2 in my index.js?
Can anyone provide some information about the command to start a server via pm2 which is locally installed?
Actually you can install it as a local project dependency and then run it directly with the path inside node_modules: node ./node_modules/pm2/bin/pm2 start
That way you can use it in a npm script for example.
I use this for a project that needs to run offline, bundling everything in the CI and running it locally then.
However as of lately I get some problems with the deamon starting under Windows that way. Not yet sure if it is a Windows problem or a problem with starting pm2 this way (as the globally installed version still works properly).
Try,npx pm2 start index.js. Read this article to learn about npx
If you are on AWS EC2 instance you can run the command from this path:
C:\Users\Administrator\AppData\Roaming\npm\pm2 start C:\project\app.js
In my case pm2 was installed but the err was pm2 npt found
so i ran pm2 command from that path and i worked
If you use npm, simply write in your package.json / "scripts":
"pm2": "npx pm2 start index.js -i max"
then run the script with npm run pm2
it will start your index.js with max available cluster workers

How can I automatically restart a Node.js application using Forever and Nodemon (Windows)

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop.
I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal.
I installed both packages globally on my Windows 10 Device.
npm install forever -g
npm install -g nodemon
I tried using the following command to launch my app:
forever start nodemon --exitcrash app.js
However, I get the following error: "nodemon does not exist"
If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash.
Am I doing something wrong? Most advice I find online is only relevant to Linux systems.
If you are already using forever, then you can get rid of nodemon. Instead you can use a combination of forever and cluster module. Simply fork the worker in case of exceptions, and it makes your app more scalable too!
If still nodemon is preferable, maybe try installing it globally using the -g flag
Forever and nodemon achieve 2 completely different objectives
nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. more about that later
Forever, on the other hand, is for making your application run as a daemon in production. And auto restart if you have uncaught exceptions.
Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart
Current norm is to use PM2

Resources