117

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

Bachalo
  • 6,965
  • 27
  • 95
  • 189
  • 7
    http://stackoverflow.com/questions/20426652/windows-auto-launch-specific-applications-on-boot How's this different than the last time you asked? – WiredPrairie Dec 07 '13 at 20:06
  • 1) Your last question is for Winblows, the answers will be radically different depending on platforms; 2) if on Linux, the easiest thing would probably be to use your distro's process manager (sysvinit, upstart, systemd) – TC1 Dec 18 '13 at 14:44
  • Unfortunately on Winblows, edited to reflect this – Bachalo Dec 18 '13 at 17:43
  • 1
    He wasn't happy with the only answer on the last question, so he re-asked it with a bounty attached. – Nahn Feb 21 '14 at 15:25
  • Winblows? Is this Slashdot in 1999? – Matthew Lock Feb 08 '17 at 00:11

13 Answers13

204

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
talles
  • 14,356
  • 8
  • 45
  • 58
  • I got an error, but it worked with `npm install qckwinsvc -g` – Adam Gerthel Jun 15 '15 at 14:55
  • Fails on win 10 pro. Event viewer: "The node-expjs-dev service terminated unexpectedly." – adi518 Mar 18 '16 at 12:22
  • @asde Thanks for reporting, I'm somewhat busy nowadays but I'll take a look when I have some time. Do you mind opening an [issue on github](https://github.com/tallesl/qckwinsvc/issues)? – talles Mar 18 '16 at 12:25
  • That was a fast reply haha! Yea, I can do that. I assume the service should open prompt with the app running in it, correct? Is it possible to config the service to run the app with Nodemon? – adi518 Mar 18 '16 at 12:26
  • Nope, the service opens no prompt. As for nodemon, it's also a no, since node-windows will just use `node` for the script (maybe that's hackable, but I have to check). – talles Mar 18 '16 at 12:38
  • 1
    Hmm, how can I debug my app without prompt? I know node-inspector might help, but debugging with CMD seems more intuitive. – adi518 Mar 18 '16 at 12:43
  • If you are running in your development environment you don't install it as a service, just run it with nodemon and the prompt as you're used to. If you want to know what's going on in another environment, such as your production server, I suggest you to use some sort of logging. – talles Mar 18 '16 at 12:54
  • FYI, as of this date, it is working for me as service on Win 10 Pro. – user555303 Sep 20 '16 at 20:28
  • 2
    I tried installing a node express app as a service, and it's running (I get the output from the console.log calls in the *.out.log file), but when I try to get a response from the server via my web browser, I just get a 404 back. – sanderd17 Oct 21 '16 at 10:53
  • Finding a solution for my own issue: I used a path `"public"` to load all static assets, which is relative the the working directory, and not the installation directory. I had to use `__dirname + "/public"` – sanderd17 Oct 21 '16 at 11:16
  • Like @sanderd17 I was trying to run an express app so wrapped and the server kept actively refusing the connection. Turned out that a relative path in my node code was failing. Error log files are output to the generated /daemon folder. – argyle Jan 26 '17 at 20:20
  • 1
    With qckwinsvc I got the messages that my service was installed and started, but using services.msc I don't see them. Also my service writes to a log at startup, but the log isn't created. – Gerry Sep 18 '19 at 17:55
119

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.

durum
  • 3,316
  • 1
  • 25
  • 30
  • 2
    unfortunately on windows 8 – Bachalo Dec 18 '13 at 17:43
  • 4
    Sir, this answer deserves more upvotes. Thanks. Also I want to say that `pm2 startup` needs one of this platform parameters: ``, ie: – sospedra Nov 21 '14 at 09:56
  • 4
    @Deerloper parameter is not needed. From their official page: `$ pm2 startup # auto-detect platform` `$ pm2 startup [platform] # render startup-script for a specific platform, the [platform] could be one of: # ubuntu|centos|redhat|gentoo|systemd|darwin|amazon` – Daniel Gabado Mar 05 '15 at 12:55
  • 1
    This is amazing and deserves to be the top answer as well as have more up votes. Thanks for this! – ILikeTurtles Jan 11 '18 at 19:53
  • @drum, Read the question again! – Siyavash Hamdi Jul 16 '19 at 14:42
  • Here is a related question and the accepted answer in 2016 is to use a Linux init system like systemd: https://stackoverflow.com/questions/4681067/how-do-i-run-a-node-js-application-as-its-own-process/28542093#28542093 It has the advantage to not use additional dependencies. If someone knows the advantages of using pm2, you can add them in comments. – baptx Jul 23 '19 at 08:27
  • 1
    @baptx If you are OK with the systemd UX the main appeal of pm2 it is that is more portable: non-systemd based distros, macOS and Windows. – durum Jul 23 '19 at 13:55
  • `pm2 startup` shows error [`Init system not found`] on Windows 10, is there a solution for that ? – Salim Shamim Jul 09 '20 at 07:57
42

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Chetan Bhasin
  • 3,503
  • 1
  • 23
  • 36
  • 2
    This works, but it is the poor man's way. The answer from talles below is better. Install it as a service. – MindJuice Dec 25 '13 at 09:04
  • 8
    @MindJuice I agree, though it's just an easy way out so I thought it was worth mentioning. – Chetan Bhasin Dec 25 '13 at 16:54
  • 16
    My application needs to run on both Linux and Windows, so this solution works better for me because it doesn't require me to require a Windows specific package in my app. Just because a solution is simple doesn't make it a bad one. – Ryan Higgins Sep 16 '16 at 18:57
  • 1
    Actually, it works only if you log in the machine. But, for scaling/CI purposes (production env), this doesn't work. – João Bruno Abou Hatem de Liz Jun 08 '20 at 20:05
  • 1
    Note that if you use relative paths in the script, the execution context will be in the startup folder. So you may wish to `cd` to the script directory before running it. – ggorlen Mar 15 '23 at 19:47
6

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.

Gust van de Wal
  • 5,211
  • 1
  • 24
  • 48
robereng
  • 61
  • 1
  • 2
5

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.

Community
  • 1
  • 1
MichaelMilom
  • 3,024
  • 1
  • 16
  • 25
3

you should try this

npm forever

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

ali ozkara
  • 5,425
  • 2
  • 27
  • 24
1

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

steampowered
  • 11,809
  • 12
  • 78
  • 98
0

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

Yar
  • 7,020
  • 11
  • 49
  • 69
0

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
0

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.

Amos
  • 2,222
  • 1
  • 26
  • 42
0

Need to create a batch file inside project folder. Write this code in batch file

@echo off 
start npm start

save batch file with myprojectname.bat

Go to run command and press window + R

Enter this command :- shell:common startup

Press ok then folder will be open.

Folder path like as C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

You will be paste your myprojectname.bat file.

You can check also. Need to system restart.

enter image description here

0

Here are the steps to set up PM2 to run on startup in Windows:

  1. Open Task Scheduler by typing "Task Scheduler" in the Start menu search bar and selecting it from the results.
  2. Click on "Create Task" in the "Actions" panel on the right.
  3. Give your task a name and description.
  4. Under the "General" tab, select "Run whether user is logged on or not" and "Run with highest privileges".
  5. Under the "Triggers" tab, click "New" and select "At startup" from the drop-down menu.
  6. Under the "Actions" tab, click "New" and select "Start a program" from the drop-down menu.
  7. In the "Program/script" field, enter the path to your PM2 installation followed by "\pm2.cmd".
  8. In the "Add arguments (optional)" field, enter "startup".
  9. Click "OK" to save your task.
Amos Chihi
  • 375
  • 3
  • 7
-2

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

Community
  • 1
  • 1
clay
  • 5,917
  • 2
  • 23
  • 21