Auto start node.js server on boot

node.js

node.js Problem Overview


Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots? I'm on Windows

node.js Solutions


Solution 1 - node.js

This isn't something to configure in node.js at all, this is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through a Windows Service.

There's this super easy module that installs a node script as a windows service, it's called node-windows (npm, github, documentation). I've used before and worked like a charm.

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\helloworld.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

p.s.

I found the thing so useful that I built an even easier to use wrapper around it (npm, github).

Installing it:

npm install -g qckwinsvc

Installing your service:

> qckwinsvc
prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed

Uninstalling your service:

> qckwinsvc --uninstall
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

Solution 2 - node.js

If you are using Linux, macOS or Windows pm2 is your friend. It's a process manager that handle clusters very well.

You install it:

npm install -g pm2

Start a cluster of, for example, 3 processes:

 pm2 start app.js -i 3

And make pm2 starts them at boot:

 pm2 startup

It has an API, an even a monitor interface:

AWESOME

Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.

Solution 3 - node.js

If I'm not wrong, you can start your application using command line and thus also using a batch file. In that case it is not a very hard task to start it with Windows login.

You just create a batch file with the following content:

node C:\myapp.js

and save it with .bat extention. Here myapp.js is your app, which in this example is located in C: drive (spcify the path).

Now you can just throw the batch file in your startup folder which is located at C:\Users%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Just open it using %appdata% in run dailog box and locate to >Roaming>Microsoft>Windows>Start Menu>Programs>Startup

The batch file will be executed at login time and start your node application from cmd.

Solution 4 - node.js

This can easily be done manually with the Windows Task Scheduler.

  • First, install forever.

  • Then, create a batch file that contains the following:

     cd C:\path\to\project\root
     call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
     exit 0
    
  • Lastly, create a scheduled task that runs when you log on. This task should call the batch file.

Solution 5 - node.js

I would recommend installing your node.js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.

Another service-related question in Stackoverflow provided a couple of (apprently) really good options. Check out How to install node.js as a Windows Service. node-windows looks really promising to me. As an aside, I used similar tools for Java apps that needed to run as services. It made my life a whole lot easier. Hope this helps.

Solution 6 - node.js

you should try this

npm forever

https://www.npmjs.com/package/forever

Solution 7 - node.js

Use pm2 to start and run your nodejs processes on windows.

Be sure to read this github discussion of how to set up task scheduler to start pm2: https://github.com/Unitech/pm2/issues/1079

Solution 8 - node.js

Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows.

Solution 9 - node.js

I know there are multiple ways to achieve this as per solutions shared above. I haven't tried all of them but some third party services lack clarity around what are all tasks being run in the background. I have achieved this through a powershell script similar to the one mentioned as windows batch file. I have scheduled it using Windows Tasks Scheduler to run every minute. This has been quite efficient and transparent so far. The advantage I have here is that I am checking the process explicitly before starting it again. This wouldn't cause much overhead to the CPU on the server. Also you don't have to explicitly place the file into the startup folders.

function CheckNodeService ()
{

$node = Get-Process node -ErrorAction SilentlyContinue

if($node)
{
    echo 'Node Running'
}
else
{
    echo 'Node not Running'
    Start-Process "C:\Program Files\nodejs\node.exe" -ArgumentList "app.js" -WorkingDirectory "E:\MyApplication"
    echo 'Node started'

}
}

CheckNodeService

Solution 10 - node.js

Simply use this, install, run and save current process list

https://www.npmjs.com/package/pm2-windows-startup

By my exp., after restart server, need to logon, in order to trigger the auto startup.

Solution 11 - node.js

Copied directly from this answer:

You could write a script in any language you want to automate this (even using nodejs) and then just install a shortcut to that script in the user's %appdata%\Microsoft\Windows\Start Menu\Programs\Startup folder

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBachaloView Question on Stackoverflow
Solution 1 - node.jstallesView Answer on Stackoverflow
Solution 2 - node.jsdurumView Answer on Stackoverflow
Solution 3 - node.jsChetan BhasinView Answer on Stackoverflow
Solution 4 - node.jsroberengView Answer on Stackoverflow
Solution 5 - node.jsMichaelMilomView Answer on Stackoverflow
Solution 6 - node.jsAli OzkaraView Answer on Stackoverflow
Solution 7 - node.jssteampoweredView Answer on Stackoverflow
Solution 8 - node.jsYarView Answer on Stackoverflow
Solution 9 - node.jsSachin HayatnagarkarView Answer on Stackoverflow
Solution 10 - node.jsAmosView Answer on Stackoverflow
Solution 11 - node.jsclayView Answer on Stackoverflow