Watch on YouTube: youtu.be/M6JaWjZKyW4

The Installer Trifecta Series

On Windows the common use case for a program is to open, use it, and close it when you're done.

This tutorial is for all the times when you want to create a system service - a program that runs in the background whether you're using it or not. Specifically, we're talking about using a NodeJS app as a service.

Pulling out the big guns

To create the installer you'll need

  • VirtualBox (or some tool with snapshots)
  • Windows 7 (or Windows 8)
  • InnoSetup 5.5.3

Note: You will definitely need a way to rollback changes as you test and retest to get your installer working and I'm not convinced that System Restore is good enough. I highly recommend that you use VirtualBox, which has much better snapshot support than Parallels or VMWare. If you ignore my warning, however, you may be able to use some of these tools to partially rollback your system:

  • regedit.exe (Registry Editor)
  • WF.msc (Windows Firewall with Advanced Security)
  • services.msc (Services)

And as part of the installation you'll need

Get the sample HelloNode app

It doesn't matter whether or not you plan to use NodeJS, get this simple app working first and then customize your script later to work for your specific use case.

Starting with something that works will help you overcome quite a few hurdles and give you faith that you can actually work through problems to get something working eventually.

  1. Go to https://github.com/coolaj86/HelloNode and click on the ZIP download button. (Or download it directory here: https://github.com/coolaj86/HelloNode/archive/master.zip)
  2. If you didn't download it to your Downloads folder, move it there (trust me, the setup is already pointing to the Downloads folder)
  3. Right-Click on HelloNode-master.zip and select Extract All
  4. Remove the trailing *HelloNode-master* so that it extracts to C:\Users\%USERNAME%\Downloads\

This app is an extremely simple webserver that tests that it can create a folder, write a file, access the internet, and then returns a response.

Using the InnoSetup Wizard

You can download InnoSetup from the official website or use the one provided in the support files for HelloNode, which is at C:\Users\%USERNAME%\Downloads\HelloNode-master\winstaller\isetup-5.5.3-unicode.exe.

Version 5.5.3 works for this tutorial. I know it works because it's what I used and so that's the version I recommend.

  1. Double-click isetup-5.5.3-unicode.exe
  2. Click run run. The file is not windows verified, but it is safe.
  3. Click yes. The installer won't work without admin privileges.
  4. Select English (or whatever) and Click OK
  5. Click next.
  6. Click I accept and then Click Next
  7. Click next to accept the default location.
  8. Click next to accept the default start menu name.
  9. Click next to accept to include the preprocessor.
  10. Click next to accept InnoSetup as the default .iss editor.
  11. Click Install to begin the installation.
  12. Click Finish to open InnoSetup

Note: Please don't make your users make so many choices. The fewer Nexts, the better. If they're that hard core of a power user they can rename the folders on their own, seriously.

Now the Welcome Wizard comes up (or you can get to it from File -> New).

  1. Choose Create a new script file using the Script Wizard
  2. Click next

Application Information

Application Name will be the default name suggested for the installation path, start menu items, etc. Set it to Hello Node for this demo.

Aside from that, none of this information is relevant to the installation of your program

Next.

Application Folder

In my experimentation I found that using Program Files caused my service not to run automatically. I wasn't able to discover why this is. No error message was produced and Event Viewer's (eventvwr.msc) Windows Logs/Applications information about nssm wasn't helpful.

  1. Change Application destination base folder to C:\ (this is important)
  2. Uncheck Allow user to change the Application Folder
  3. Remove the spaces from Application folder name (it should be just HelloNode)

If I find that a future version of nssm or node or whatever doesn't work from Program Files begins to work, I'll change my advice on the matter.

Next.

Application Files

Application main executable file

For the webapp example there's not much of a main executable file - being that the server is run as a service and the client is a browser - but since Chrome and MSIE both have app modes, we can specify a batch file that runs the best available browser on the system.

  • HelloNode-master\winstaller\winstart-browser.bat

Even if you know you have Chrome (say you install it as part of the package), The batch file is still useful for passing the necessary arguments.

Otherwise, select The application doesn't have a main executable file

Add files

For Hello Node you need to add

  • HelloNode-master\winstaller\winstart-server.bat
  • HelloNode-master\winstaller\winstart-browser.bat
  • HelloNode-master\winstaller\nssm.exe
  • HelloNode-master\winstaller\nssm-x86.exe
  • HelloNode-master\winstaller\msie-app.hta
  • HelloNode-master\winstaller\msie-app-secure.hta
  • HelloNode-master\winstaller\web-trifecta-badge.ico
  • HelloNode-master\winstaller\node-v0.10.4-x64.msi

Or, in other words, basically everything in winstaller except isetup-xyz.exe and hello-node.iss

All of these files are windows-only and not useful for OS X or Linux.

Add folders

The HelloNode folder has all of the other os-inspecific files

  • HelloNode-master\HelloNode\

(Yes, you want to add the folders and files recursively)

Next.

Application Icons

I'd recommend alleviating the user of unnecessary choices. Put it there, or don't. If they're a power user they'll add it or delete it to fit their needs. Don't make the user click yet another next!

We'll be editing that line anyway.

(I unchecked everything)

Next

Application Documentation

The License, Before, and After files are just plain text files.

I recommend including none of them, but License if you must.

None are used in this example.

Next.

Setup Languages

Next.

Compiler Settings

Just set the Custom compiler output folder to the Desktop.

I like to set the Compiler output base file name to include the the name of the app, such as HelloNodeInstaller.

You can optionally set a Custom Setup icon file, such as the one provided.

Inno Setup Preprocessor

Next

Finish

Finish.

Don't compile the script now.

Editing the Script

defines

The #defines are just for convenience, but I'd recommend using them.

I added the following (and replaced existing occurrences of the strings)

#define MyAppShortName "HelloNode"
#define MyAppLCShortName "hellonode"
#define MyAppIcon "web-trifecta-badge.ico"

#define NSSM32 "nssm-x86.exe"
#define NSSM64 "nssm.exe"
#define NSSM "nssm.exe"

#define NODE32 "node-v0.10.4-x86.msi"
#define NODE64 "node-v0.10.4-x64.msi"
#define NODE "node-v0.10.4-x64.msi"

#define USERPROFILE "C:\Users\coolaj86"

[Icons]

Folders are just more confusion. I drop the {group} and add IconFilename to the Start Menu icon.

Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\{#MyAppIcon}"

; Here's an example of how you could use a start menu item for just Chrome, no batch file
;Name: "{group}\{#MyAppName}"; Filename: "{pf32}\Google\Chrome\Application\chrome.exe"; Parameters: "--app=http://localhost:5566 --user-data-dir=%APPDATA%\{#MyAppShortName}\"; IconFilename: "{app}\{#MyAppIcon}"

[Files]

No changes needed here.

[Run]

I added runhidden to the run after install so that the cmd.exe window doesn't show up

; postinstall
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: shellexec postinstall skipifsilent runhidden

All of the sections below must be added manually

First, NodeJS needs to be installed

Filename: "{sys}\msiexec.exe"; Parameters: "/passive /i ""{app}\{#NODE}""";

Also, we need to add firewall rules to allow us to access the service (via http) as well as to allow it to access the Internet (for updates or whatever).

The firewall rules actually apply to node.exe since it is the executable that runs the the script.

Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node In"" program=""{pf64}\nodejs\node.exe"" dir=in action=allow enable=yes"; Flags: runhidden;
Filename: "{sys}\netsh.exe"; Parameters: "advfirewall firewall add rule name=""Node Out"" program=""{pf64}\nodejs\node.exe"" dir=out action=allow enable=yes"; Flags: runhidden;

And lastly we need to add the app as a system service and to start it

Filename: "{app}\{#NSSM}"; Parameters: "install {#MyAppShortName} ""{pf64}\nodejs\node.exe"" ""{app}\bin\server.js"" ""5566"""; Flags: runhidden;
Filename: "{sys}\net.exe"; Parameters: "start {#MyAppShortName}"; Flags: runhidden;

There is a module, node-windows, that creates system services, but I wasn't able to get it working.

Build & Test

Hit the green arrow. It will create the Installer and install it.

Once installed, visit http://localhost:5599 and make sure that it returns Hello World 1 (or 2 or 3...). If it's doing that, the firewall rules and file permissions are all correct.

If you do have any trouble with .iss file that you're creating, try using the one that's included (HelloNode-master\winstaller\hello-node.iss) instead.

Modifying for yourself

Once you've run the HelloNode installed and working, then it's time to try new things - but only one at a time. This install process is really fickle (YMMV). If you make 10 changes to the script and try it out, it probably won't work.

Resources


By AJ ONeal

If you loved this and want more like it, sign up!


Did I make your day?
Buy me a coffeeBuy me a coffee  

(you can learn about the bigger picture I'm working towards on my patreon page )