Dara Vichit
Dara Vichit

Reputation: 620

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?

Upvotes: 7

Views: 33811

Answers (4)

Hifihedgehog
Hifihedgehog

Reputation: 121

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/

Upvotes: 1

j_d_b
j_d_b

Reputation: 647

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.

Upvotes: 1

Himani Agrawal
Himani Agrawal

Reputation: 1272

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

Upvotes: 14

Amulya Kashyap
Amulya Kashyap

Reputation: 2373

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.

Upvotes: -4

Related Questions