Node.js application as a Windows service

Zápisník experimentátora

Hierarchy: Node.js

In this article, we'll show you how to install the Node.js application on Windows as a service. This ensures that the application will start when you turn on your computer. This procedure uses the PM2 package.

Installing Node.js

Download the installer from https://nodejs.org/. You have several options to choose from. Normally, try installing the version where you see the Recommended For Most Users warning.

PM2

PM2 (https://pm2.keymetrics.io/) is a process manager for programs written in Node.js.

Setting the environment variable

When installing on Windows, it is important to first set the PM2_HOME environment variable to a directory that is visible to all processes. It is a good idea to use a directory that is easy to find when managing apps that are running automatically. For example, if you have apx installed in d:\app\apx, set this variable to d:\app\pm2. Be sure to create this directory on disk.

  • d:
    • app
      • pm2
      • apx

The setting of the variable will vary depending on the version of Windows on the server. Here are some examples.

  • Windows 8 - Right-click the start icon and select System. Then Advanced system settings. On the Advanced tab there is the Enviroment variables button. Add PM2_HOME to System variables.
  • Windows Server 2016 - Right-click the start icon and select System. Then Advanced system settings. On the Advanced tab there is the Enviroment variables button. Add PM2_HOME to System variables.

Installation

Run cmd.exe as admin.

npm install pm2 -g
pm2 install pm2-logrotate

After installation, you will see this on the command line. The pm2-logrotate module is running and the application list is empty.

Several files have been created in the PM2_HOME directory. See pm2.log for additional startup information. When you add some applications to PM2, their list and environment settings are written to dump.pm2. Logs will appear in the logs directory. If there is any problem, then be sure to check the contents of this directory.

At this point, we have everything you need to use PM2. However, PM2 does not start automatically when the computer restarts. It is necessary to add Windows service, which starts it at startup.

Windows service

The official documentation is very brief at this point and does not include up-to-date information. The recommended package is pm2-windows-service. However, it is not maintained for a long time and therefore it is necessary to use @innomizetech/pm2-windows-service instead. The original package contains links to outdated packages and gets stuck during installation on newer Windows.

The pm2-service-install will ask a few questions. Answer no to all of them. Although the program recommends this, you only need to set the PM2_HOME variable to run properly. This setting variables affects running applications via PM2 and it is better to control all applications only through PM2. A relatively long directory path will also be displayed during installation. It's a good idea to remember it, because there is another log in that directory that contains information about how Windows started. Usually you do not need it, but sometimes it happens that Windows reboots or only requires services to be restarted, and if you have bugs in your applications and are not well designed to handle reboots, then you can find useful information about reboots in this log.

npm i @innomizetech/pm2-windows-service -g
pm2-service-install

You are now ready to run the application after Windows Node.js restarts. It is a good idea to add individual applications to PM2 using the generated ecosystem.config.js file. In this file you set all the parameters for the application you need. In the directory where you have the application installed, add it to PM2 with the command.

pm2 start ecosystem.config.js
pm2 save

An example of a file that I use to make an application reboot itself on changes, ignore changes in some directories, and not run like like crazy when exceptions happens is in the following sample.

module.exports = {
  apps: [{
    name: 'optivusapi',
    script: './bin/www',
    watch: true,
    watch_delay: 30000,
    ignore_watch: ["node_modules", "logs", "log"],
    restart_delay: 6000,
    max_memory_restart: '300M',
    env: {
      NODE_ENV: 'production'
    }
  }]
};

 


03.04.2020


Menu