pm2 添加命令行参数到nodejs 应用
Add Node.js Arguments to Your App
There are multiple use cases why you want to customize the default V8 configuration to your needs. In our case, we need to reduce the default garbage collector size from 1.5 GB to 500 MB. That’s because our droplet doesn’t have that much memory available :)
Add Argument via CLI
Starting your apps from command line doesn’t exclude you from the group that is able to benefit from this feature. Tell PM2 that you’re passing specific Node.js arguments by using the parameter --node-args="your arguments with values".
pm2 start my_app.js --node-args="--max_old_space_size=500"
Of course, you can pass multiple Node.js arguments within your command by separating the values with a space. The following example will adjust the garbage collector size to 500 MB (max_old_space_size) and also enable ES2015 features (harmony). Please bear with me that we’re using the harmony flag for illustration. If you’re using Node 4 or later, you’ve already enabled those features.
Add multiple Node.js arguments
pm2 start my_app.js --node-args="--max_old_space_size=500 harmony"
Just to make sure: if you want to pass multiple arguments when starting your app, separate the values by a single space.
Add Argument via JSON File
PM2 allows you to define your app’s configuration within a JSON file. This way, you don’t need to type and remember the commands when updating or starting new apps. The complete configuration is stored within the JSON file.
Of course you can define Node.js V8 arguments by adding the node_args property. Notice the difference between command line (node-args) and JSON file (node_args).
{
"apps": [
{
"name": "futurestudio-homepage",
"script": "./homepage/server.js",
"node_args": "--max_old_space_size=500"
}
]
}
When using the JSON file, you need to add two leading dashes to the argument and its value. Also, you can pass multiple arguments using an array value for the node_args property within the JSON file. The following code snippet illustrates how to add two arguments.
Add multiple Node.js arguments
{
"apps": [
{
"name": "futurestudio-homepage",
"script": "./homepage/server.js",
"node_args": ["--harmony", "--max_old_space_size=500"]
}
]
}
When passing multiple arguments to Node.js for node_args, you need to use string values and separate them by comma. PM2 will then parse the values and pass them forward to Node.