Skip to main content

@nestjs-mod/pm2

Production process manager for Node.JS applications with a built-in load balancer for NestJS-mod (Generator ecosystem.config.json for https://www.npmjs.com/package/pm2)

NPM version monthly downloads Telegram Discord

Installation

npm i --save pm2 dotenv @nestjs-mod/pm2

Modules

LinkCategoryDescription
Pm2infrastructureProduction process manager for Node.JS applications with a built-in load balancer for NestJS-mod (Generator ecosystem.config.json for https://www.npmjs.com/package/pm2)

Modules descriptions

Pm2

Production process manager for Node.JS applications with a built-in load balancer for NestJS-mod (Generator ecosystem.config.json for https://www.npmjs.com/package/pm2)

Use in NestJS-mod

An example you can see the full example here https://github.com/nestjs-mod/nestjs-mod-contrib/tree/master/apps/example-terminus.

import { PACKAGE_JSON_FILE, ProjectUtils, bootstrapNestApplication } from '@nestjs-mod/common';
import { ECOSYSTEM_CONFIG_FILE, Pm2 } from '@nestjs-mod/pm2';
import { join } from 'path';

const rootFolder = join(__dirname, '..', '..', '..');
const appFolder = join(rootFolder, 'apps', 'example-terminus');

bootstrapNestApplication({
globalEnvironmentsOptions: { debug: true },
globalConfigurationOptions: { debug: true },
modules: {
system: [
ProjectUtils.forRoot({
staticConfiguration: {
applicationPackageJsonFile: join(appFolder, PACKAGE_JSON_FILE),
packageJsonFile: join(rootFolder, PACKAGE_JSON_FILE),
envFile: join(rootFolder, '.env'),
},
}),
],
infrastructure: [
Pm2.forRoot({
configuration: {
ecosystemConfigFile: join(rootFolder, ECOSYSTEM_CONFIG_FILE),
applicationScriptFile: join('dist/apps/example-terminus/main.js'),
},
}),
],
},
});

After connecting the module to the application and npm run build and starting generation of documentation through npm run docs:infrastructure, you will have new files and scripts to run.

New scripts mostly package.json

{
"scripts": {
"_____pm2 prod infra_____": "_____pm2 prod infra_____",
"pm2:start": "./node_modules/.bin/pm2 start ./ecosystem.config.json",
"pm2:stop": "./node_modules/.bin/pm2 delete all"
},
"scriptsComments": {
"pm2:start": ["Launch all applications in PM2 mode"],
"pm2:stop": ["Stop all applications in PM2 mode"]
}
}

Config file for start all built applications ecosystem.config.json

{
"apps": [
{ "name": "terminus", "script": "node ./dist/apps/example-terminus/main.js", "node_args": "-r dotenv/config" }
]
}

Configuration

KeyDescriptionConstraintsDefaultValue
ecosystemConfigFileConfiguration file for PM2isNotEmpty (ecosystemConfigFile should not be empty)--
applicationScriptFileApplication script file name for runisNotEmpty (applicationScriptFile should not be empty)--
autorestartEnable or disable auto restart after process failure.optional--
nameAn arbitrary name that can be used to interact with (e.g. restart) the process later in other commands. Defaults to the script name without its extension (eg “testScript” for “testScript.js”)optional--
scriptThe path of the script to runoptional--
argsA string or array of strings composed of arguments to pass to the script.optional--
interpreter_argsA string or array of strings composed of arguments to call the interpreter process with. Eg “–harmony” or [”–harmony”,”–debug”]. Only applies if interpreter is something other than “none”.optional--
cwdThe working directory to start the process with.optional--
outputThe path to a file to append stdout output to. Can be the same file as error.optional--
errorThe path to a file to append stderr output to. Can be the same file as output.optional--
log_date_formatThe display format for log timestamps (eg “YYYY-MM-DD HH:mm Z”). The format is a moment display format.optional--
pidThe path to a file to write the pid of the started process. The file will be overwritten. Note that the file is not used in any way by pm2 and so the user is free to manipulate or remove that file at any time. The file will be deleted when the process is stopped or the daemon killed.optional--
min_uptimeThe minimum uptime of the script before it’s considered successfully started.optional--
max_restartsThe maximum number of times in a row a script will be restarted if it exits in less than min_uptime.optional--
max_memory_restartIf sets and script’s memory usage goes about the configured number, pm2 restarts the script. Uses human-friendly suffixes: ‘K’ for kilobytes, ‘M’ for megabytes, ‘G’ for gigabytes’, etc. Eg “150M”.optional--
node_argsArguments to pass to the interpreteroptional-r dotenv/config-
timePrefix logs with timeoptional--
wait_readyThis will make PM2 listen for that event. In your application you will need to add process.send('ready'); when you want your application to be considered as ready.optional--
kill_timeoutThe number of milliseconds to wait after a stop or restart command issues a SIGINT signal to kill the script forceably with a SIGKILL signal.optional--
restart_delayNumber of millseconds to wait before restarting a script that has exited.optional--
interpreterThe interpreter for your script (eg “python”, “ruby”, “bash”, etc). The value “none” will execute the ‘script’ as a binary executable.optional--
exec_modeIf sets to ‘cluster’, will enable clustering (running multiple instances of the script).optional--
instancesHow many instances of script to create. Only relevant in exec_mode ‘cluster’.optional--
merge_logsIf true, merges the log files for all instances of script into one stderr log and one stdout log. Only applies in ‘cluster’ mode. For example, if you have 4 instances of ‘test.js’ started via pm2, normally you would have 4 stdout log files and 4 stderr log files, but with this option set to true you would only have one stdout file and one stderr file.optional--
watchIf set to true, the application will be restarted on change of the script file.optional--
forceBy default, pm2 will only start a script if that script isn’t already running (a script is a path to an application, not the name of an application already running). If force is set to true, pm2 will start a new instance of that script.optional--
ignore_watch-optional--
cron-optional--
execute_command-optional--
write-optional--
source_map_support-optional--
disable_source_map_support-optional--
envThe environment variables to pass on to the process.optional--

Back to Top

License

MIT