How to properly run NodeJs on Windows Server in production - node.js

I am having difficult times, trying to make my NodeJS scripts run on windows server 2012. Or more precisely, to make it robust.
I have installed PM2, whic his great, also added service for windows startup which works fine, but now I found biggest issue I can't solve.
When windows server user start pm2 start, directly on server or through ssh, when logging out, all pm2 scripts are gone.
I've tried to look into pm2-windows-service but that seems inconsistent, when I restart service, it works fine, but sometimes I need to manually reload o restart only 1 script and then whole list of pm2 scripts gets somehow detached or attached to user, so when I log out from server it's all gone again.
I can't find solution to have watcher/autorestart on scripts, and make them run as a service regardless of user being logged in/out.
There must be solution for running multiple nodejs scripts on windows ?

Once you launch your NodeJS processes, you should run pm2 save command. That way, your stated scripts are saved.
Your attached/dettached problems may be due a relative path configuration in the service env variables. Try moving to an absolute path.
Note: PM2 recommendation is to launch commands with an admin-privileged user; otherwise inconsistences may appear.
Note (2): Bare in mind that PM2 on Windows has some issues. For example, if you restart your server, most of the times processes dissapear from pm2 ls (they noted that Windows does not have the feature to restore saved instances). If you saved them, pm2 resurrect will restore them.

Related

Node.js Command Prompt Issue

I don't believe this behavior is correct at all, but please correct me if I'm mistaken. So I have an Angular 2/Ionic 2 app created all through the Node.js command prompt... the commands all work fine, up until I execute ng serve, after that I can't type at all into the command prompt. Only way I can type is if I close out of the command prompt and restart (less than ideal). Is this normal behavior? Or only occurring on my machine?
ng serve launches a basic web server for you using which your static files are served. It also listens for updates to your project files and if any changes are detected it bundles the project again and reloads the browser tab automatically. So it's important that it keeps running when you're developing your application.

How to run a node.js file as a background process on a Windows server?

I was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.

How to make a node application on Digitalocean run constantly?

I recently got a node application up and running on digitalocean's droplet. Everything is working properly, if I go forever on the server javascript file I can navigate to the site and it works.
If I close the terminal window in which I am ssh-ing, the site is no longer available, which is expected of course. But my question is, how can I run the forever as a daemon of sorts?
I could not find the answer anywhere, which is really strange...hope somebody here might be able to help me
Generally forever should continue running if you set it up as a background task and don't close it. Unfortunately if the server needs to restart (which can happen often), you need something to get it started again.
An alternative to forever is pm2, pretty much the same thing. There is a tutorial on how to set up your production server here. Here is an excerpt from the site for just the pm2 part, as I assume you got everything else sorted:
Install PM2
Now we will install PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).
We will use Node Packaged Modules (NPM), which is basically a package manager for Node modules that installs with Node.js, to install PM2 on our app server. Use this command to install PM2:
sudo npm install pm2 -g
Manage Application with PM2
PM2 is simple and easy to use. We will cover a few basic uses of PM2.
Start Application
The first thing you will want to do is use the pm2 start command to run your application, hello.js, in the background:
pm2 start hello.js
This also adds your application to PM2's process list, which is outputted every time you start an application
PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots. You must also specify the platform you are running on, which is ubuntu, in our case:
pm2 startup ubuntu
The last line of the resulting output will include a command (that must be run with superuser privileges) that you must run:
Output:
[PM2] You have to run this command as root
[PM2] Execute the following command :
[PM2] sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u sammy
Run the command that was generated (similar to the highlighted output above) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u sammy

nodejs app with forever in cronjob does not work after reboot

I created a cronjob with sudo crontab -u USERNAME -e and tried to start my node.js app after each reboot.
It had worked well at another server.
But this time, I installed the Node.js via nvm
Then tried to run the same cronjob lines, but every time after reboot it failed. I tried to figure out all the related folders and tried again to run as different lines. When I check with forever list, server shows that no process is running.
#reboot /home/USERNAME/.npm/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
#reboot /home/USERNAME/.npm/forever start /home/USERNAME/APPNAME/app
#reboot /usr/local/bin/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
I think that the reason is somehow related with nvm. But I am not sure of that. I don't want to do anything unnecessary unless I am fully sure about it.
Edited July 26, 2015
Though I have used the npm's forever module to deploy nodejs production apps, I really do not see the need to use it on linux based servers as there are so many system level alternatives available.
One of them is upstart. It will help you run your scripts as system level services. Amazon Web Services also use upstart in their Elastic Beanstalk nodejs tiers to keep nodejs apps running forever.
If you really just need to get down to it, here is a link to run your nodejs app as an upstart service.
However, it is not limited to deploying nodejs apps only and if you learn upstart, you will be able to do a lot of things with it. Here is a link for that as well.
If your original approach was preferable (as it was in my case) you can fix your scheduled cron job by explicitly passing the path to node like this: (found in this answer)
#reboot /root/.nvm/versions/node/v7.1.0/bin/node /root/.nvm/versions/node/v7.1.0/bin/forever start /var/www/server.json
Apparently NVM works in magic by setting up some system paths which aren't setup at the moment the cron jobs run.

Keeping node.js running

During development it would be great to have node.js restarted automatically when changes occur in the applications directory. When the application is in production, node could be start just like normally. When the server is rebooted with or without intention, node needs obviously to be restarted.
I've heard about and tried both forever and supervisor. Supervisor works good, but I can't make it to start during boot. Forever on the other hand ignores any changes in my directory.
Are there alternatives that are available that would do all this for me?
Thanks
Here's what I ended up doing:
Below code was put in /etc/init/myApplication.conf:
# Enter below
respawn
console none
start on runlevel [2345]
stop on runlevel [06]
script
sudo always /var/www/backend/app.js > /var/www/backend/nodelog.log
end script
Always checks for changes in my node directory while this script ensures that always is started on boot.
I've recently started using Foreman to manage my apps. You can export from Foreman to Upstart which should solve your problem with handling reboots, etc. in a production environment (although I haven't done this myself yet.)
For automatically updating files on change, check out always. (Then run always from your local Procfile through Foreman - that's my setup.)
I don't think you are likely to find anything that automatically starts node on boot, but depending on your OS, this isn't very difficult. Could you tell us what your OS is?

Resources