How to start Node.js app windowless in Windows
From [https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/]
Node.js is getting more popular by the day. It breaks JavaScript free from the browser. I would love to auto start an application – much like a Windows service – without keeping a console window open. There are many programs / services to wrap node applications into an executable, but I like to stick with just Node. This small trick will start your Node application windowless on Windows.
VBS to the rescue
I’ve created a small Visual Basic Script called app.vbs that will start your node application within a hidden window:
CreateObject("Wscript.Shell").Run "node app.js", 0
The little zero at the end does the trick. The string is the command to start the node process with the app.js. The .vbs extension is executable in Windows.
Autostart
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\ directory and add a shortcut to the app.vbs file.
Proof of concept
So let’s build a small app to proof that it works. The following script will create a web server and log all the requests to a debug file. We’ll stream the file back. The /clear will clear the log file. The /kill will stop the app – this is very important, because the app will be started without a window. This will give us an easy way to shut it down.
var app = require('express')(); var fs = require('fs'); var util = require('util'); var logFileName = __dirname + '/debug.log'; //log number of requests var nrOfRequests = 0; //capture all requests app.get('*', (req, res) => { nrOfRequests++; console.log(`${nrOfRequests}. [${req.url}] on ${new Date()}`); res.sendFile(logFileName, () => { if (req.url == '/kill') setTimeout(() => process.exit(), 500); else if (req.url == '/clear') { fs.createWriteStream(logFileName, { flags: 'w' }) } }); }); //log to file var logFile = fs.createWriteStream(logFileName, { flags: 'a' }); var logProxy = console.log; console.log = function (d) { // logFile.write(util.format(d || '') + '\n'); logProxy.apply(this, arguments); }; //start listening var port = process.env.PORT || 5000; app.listen(port, function () { console.log('Application started on ' + new Date()); console.log("Listening on " + port); });
Try it out!
Don’t forget to execute a npm install express to install the web server! Always make sure that the node app.js works. The .vbs will not create a window, so you won’t see if it crashes.
- Execute the app.vbs. This will create a process:
- Open up the app in your browser http://localhost:5000 and you’ll see:
- Play around with it and kill the application with http://localhost:5000/kill. This request stops the node application. You’ll see that it is no longer present in the task manager.
Wrap-up
So, that’s it. Pretty easy. Remember: this only works under Windows! Also remember: windowless applications can’t be terminated using the CTRL+C command. Killing the process in the task manager will.
Thanks for the guide. Used this solution for a while.
ReplyDeleteI recently decided to create the following module that can help others to setup a project that needs a silent execution of nodejs:
create-windowless-app:
https://www.npmjs.com/package/create-windowless-app
This module:
1. Create a "hello world" project structure (javascript or typescript) that compiles into an **exe** file (using nexe, with configurable nodejs version)
2. Compiles a **c#** launcher (with an option to provide an icon) that can start the project exe in a silent mode. (source included)