Automatically start node.js server on boot

Can any of the node.js experts tell me how I can configure node JS to autoload the server when my computer boots up? I am on Windows

+69
Dec 07 '13 at 19:28
source share
10 answers

This is not at all what you need to configure in node.js, it is purely OS responsibility (Windows in your case). The most reliable way to achieve this is through the Windows service.

In this super-lightweight module that installs node script as a Windows service, it is called node-windows ( npm , github ,. I used this 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(); 



ps

I found the thing so useful that I built an even more convenient wrapper around it ( npm , github ).

Installation:

 npm install -g qckwinsvc 

Service Installation:

 > 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 

Removing a service:

 > qckwinsvc --uninstall prompt: Service name: [name of your service] prompt: Node script path: [path of your node script] Service stopped Service uninstalled 
+144
Dec 22 '13 at 0:06
source share

If you are using Linux, MacOS or Windows pm2 is your friend. This is a process manager that handles clusters very well.

You install this:

 npm install -g pm2 

Start a cluster, for example, from 3 processes:

  pm2 start app.js -i 3 

And make pm2 runs them on boot:

  pm2 startup 

It has an API, even a monitor interface :

Awesome

Go to github and read the instructions . It is easy to use and very convenient. The best thing ever .

+67
Dec 18 '13 at 14:39
source share

If I am not mistaken, you can start the application using the command line and, therefore, also use a batch file. In this case, it is not very difficult to start it using Windows login.

You simply create a batch file with the following contents:

 node C:\myapp.js 

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

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

Just open it using% appdata% in the dailog launch box and search in> Roaming> Microsoft> Windows> Start Menu> Programs> Start

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

+28
Dec 19 '13 at 13:56 on
source share

I would recommend installing your node.js application as a Windows service, and then installing the service to start at startup. This should make it easier to control the launch action using the Windows Services snap-in, and not to add or remove batch files in the Startup folder.

Another service-related issue at Stackoverflow provided a couple (apparently) of really good options. Check How to install node.js as a Windows service . node-windows looks very promising to me. As an aside, I used similar tools for Java applications that needed to be run as services. It made my life a lot easier. Hope this helps.

+3
Dec 21 '13 at 16:17
source share

you should try this

npm forever

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

+3
Nov 27 '16 at 19:19
source share

This is easily done manually using the Windows Task Scheduler.

  • Install forever first.
  • 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 
  • Finally, create a scheduled task that starts when you log in. This task should invoke the batch file.

+2
Oct 12 '15 at 13:46
source share

Use pm2 to start and run nodejs processes in windows.

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

+1
May 11 '16 at 15:07
source share

Here is another solution I wrote in C # to automatically start my own node server or pm2 server on Windows.

0
Dec 02 '15 at 18:00
source share

I know that there are several ways to achieve this in accordance with the solutions mentioned above. I have not tried all of them, but some third-party services do not have a clear idea of ​​what tasks are performed in the background. I achieved this with a powershell script similar to the one referred to as the Windows batch file. I scheduled it using the Windows Task Scheduler to run every minute. It has been pretty effective and transparent so far. The advantage I have here is that I check the process explicitly before starting it again. This will not overload the processor on the server. Also, you do not need to explicitly put the file in 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 
0
Apr 25 '18 at 13:05
source share

Copied directly from this answer :

You can write a script in any language you want to automate (even using nodejs), and then just set the shortcut to this script in the user% appdata% \ Microsoft \ Windows \ Start Menu \ Programs \ Boot folder

-one
Dec 19 '13 at 13:39 on
source share



All Articles