• Stars
    star
    486
  • Rank 90,527 (Top 2 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created over 5 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Install PM2 offline as a service on Windows or Linux. Mostly designed for Windows.

pm2-installer

pm2-installer is designed to automate installation of pm2 as a service, particularly on Windows, even in environments without Internet access.

Windows Support

Unfortunately, PM2 has no built-in startup support for Windows. PM2's documentation recommends using either pm2-windows-service or pm2-windows-startup. However, both of these projects have some real drawbacks.

pm2-windows-startup adds an entry to the registry to start pm2 after user login. Because it does not create a service, PM2 will not be running until a user has logged into the user interface, and will halt when they log out. It has not been updated since 2015.

pm2-windows-service uses node-windows to create a service that runs pm2. This is a much better approach, but it hasn't been maintained since 2018, has outdated dependencies that cause crashes on setup, and currently fails to run properly on Node 14. It also runs the service as the the Local System user instead of Local Service.

This project creates its own Windows Service using the current version of node-windows and a series of PowerShell scripts inspired by this excellent gist by @maxfierke & @mauron85.

When running on Windows, pm2-installer will:

  • Configure npm to keep its global files in C:\ProgramData\npm, instead of keeping them in the current user's %APPDATA%
  • Install pm2 globally, using an offline cache if necessary
  • Create the C:\ProgramData\pm2 directory and set the PM2_HOME environmental variable at the machine level
  • Set permissions both the new npm and pm2 folders so that the Local Service user may access them
  • Leverage node-windows to install a new Windows service
  • Use PowerShell to configure the service to run as the Local Service user (due to node-windows#89)
  • Confirm the service is running properly
  • Install the pm2-logrotate module so that log files don't fill up the disk

After that, pm2 will be running in the background the Local Service user. It will persist across reboots and continue running regardless of which user is logged in. To add your app, run pm2 start app.js from an admin command line interface. Make sure to run pm2 save to serialize the process list.

Install

Download the latest version here.

Copy the entire pm2-installer directory onto the target machine, then run:

npm run setup

On Windows, the setup script assumes you have already configured npm to use prefix and cache directories in a location accessible to the Local Service user. If not, it will issue a warning and ask if you're sure you'd like to proceed. To set up npm automatically, run configure first:

npm run configure
npm run setup

That's it.

NOTE: Running npm install is not necessary, unless you're developing pm2-installer itself and want to lint the project.

Offline Install

pm2-installer is also designed to function without an internet connection. It does this by creating a cache on an internet-connected build machine, then installing from that cache when run on the offline deployment machine.

On an internet-connected build machine running the same OS as the deployment target, run the following:

npm run bundle

This will populate the cache in the project's directory with the resources required to install offline. Transfer the entire pm2-installer directory onto the deployment target, then run:

npm run setup

pm2-installer will check if it can contact the npm registry and install online if possible, or use the offline cache if not.

Windows Install

There are a couple additional challenges when installing on a fresh Windows machine. The npm global directory is not accessible to other users by default, which means the Local Service user will not be able to locate the pm2 executable. Additionally, if the machine's PowerShell execution policy is Undefined or Restricted, invoking pm2 in PowerShell will fail- even though the setup script unblocks pm2.ps1.

pm2-installer includes two additional scripts to automatically fix the above issues. Invoking npm run configure will create the C:\ProgramData\npm\, and set npm to use prefix and cache locations in that directory. Running npm run configure-policy checks the machine's PowerShell execution policy and if it is either undefined or Restricted, updates it to RemoteSigned.

Open an elevated terminal (e.g. right click and select "Run as Admin") and run the following commands first:

npm run configure
npm run configure-policy

Then, run npm run setup to install the pm2 service.

Additional context for Windows installations

  • The pm2 service runs as the Local Service user. To interact with pm2, you need to use an elevated terminal (e.g. right click and select "Run as Admin") before running any commands that interface with the service, e.g. pm2 list.
  • If you update node and npm, make sure to either manually re-configure your npm & node installations or run npm run configure again.
  • This project does not currently support nvm for windows. It requires a standard node installation.

Removal

To remove the pm2 service, run:

npm run remove

This will remove the service and completely uninstall pm2.

If you used the configure script on Windows to configure npm, you can revert those settings by running:

npm run deconfigure

License

MIT Β© Jesse Youngblood

More Repositories

1

env-smart

Zero-dependency Node library for using .env files with default values and types in TS/JS
TypeScript
20
star
2

simple-hmac-auth

Protocol specification and Node library designed to make building APIs that use HMAC signatures simple
TypeScript
15
star
3

wiegand-encoder

Encode and decode 26-bit, 34-bit, or 38-bit Wiegand protocol credentials for communicating with access control systems in TypeScript or JavaScript
TypeScript
10
star
4

GroceryCloud

Swift 3 sample grocery app that uses iCloud document storage to sync across devices
Swift
7
star
5

boilerplate-typescript-package

Boilerplate for a TypeScript package, with CI/CD for npm & gpr
TypeScript
4
star
6

encrypted-env

Encrypt and decrypt env config files quickly and easily
TypeScript
4
star
7

simple-hmac-auth-express

Express middleware for creating APIs that implement hmac signatures
TypeScript
4
star
8

boilerplate-typescript-service

Boilerplate for a TypeScript Node service
Dockerfile
3
star
9

modern-error

A modern take on JavaScript errors
JavaScript
3
star
10

workflow-test

2
star
11

untappical

ICS calendar feed server for @Untappd checkins 🍻
JavaScript
2
star
12

FauxConsole

JavaScript library that provides a console for debugging web apps in archaic embedded environments without one
JavaScript
2
star
13

eslint-config

ESLint shareable config for TypeScript or JavaScript by @jessety
JavaScript
2
star
14

boilerplate-node-offline-bundle

Boilerplate scripts to make installing a Node service offline on Windows or Linux easy
JavaScript
2
star
15

simple-hmac-auth-objc

Objective-C iOS framework for interfacing with APIs that implement hmac signatures
Objective-C
2
star
16

simple-hmac-auth-koa

Koa middleware for creating APIs that implement hmac signatures
TypeScript
2
star
17

simple-log-colors

Extremely lightweight zero-dependency library for coloring Node terminal output
TypeScript
2
star
18

coverage-test

TypeScript
1
star
19

simple-hmac-auth-php

PHP library for interfacing with APIs that implement hmac signatures
PHP
1
star
20

brb-site

Simple web server that returns a maintenance message
JavaScript
1
star
21

tail-json

Tail a file. If a line is JSON, pretty-print it. Otherwise, just print it. Basically just boilerplate.
TypeScript
1
star
22

codeql-test

Testing GitHub's code scanning
TypeScript
1
star
23

prettier-config

Prettier config by @jessety
JavaScript
1
star
24

simple-hmac-auth-swift

Swift framework for interfacing with APIs that implement HMAC signatures
Swift
1
star
25

github-webhook

A minimal GitHub webhook listener that executes a specified command when push occurs on the default branch, or when a release is published.
JavaScript
1
star