PM2 — How to Start Your App With Node.js V8 Arguments

You’ve already noticed that we’re adoring fanboys of PM2 to manage our Node.js processes. We heavily rely on PM2 to keep the Future Studio platform permanently available for you. That also includes customizations related to Node.js (like memory limit) itself. PM2 allows you to pass arguments to Node.js and manipulate default values. This guide will show you how to start your app and simultaneously pass arguments to Node.js V8.

PM2 Series Overview

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.

Example: Reduce Node’s Default Garbage Collector Size

Lately, we’ve noticed that our droplet’s memory is beyond limits. The swap file grows constantly and we didn’t find the issue that causes this behavior. Finally, we deployed an update to the blog and saw that both, memory and swap file, were freeing lots of space. That means, we have to keep watching the Node.js processes and why it takes so much memory.

Then, we’ve read an article within the RisingStack blog about how to find a memory leak in Node.js. There was the hint: the Node.js garbage collector by default starts to act if Node is using at least 1.5 GB of memory. The solution then was kind of obvious: we need to reduce the value at which Node will start cleaning the kitchen.

We updated our app’s JSON files to customize the garbage collector value of Node.js to 500 MB using the node_args property and setting the --max_old_space_size=500 arguments.

What Comes Next

Using PM2 as your process manager for Node.js apps allows you to customize your default values for Node V8. This tutorial showed you how to pass Node.js arguments to PM2 via command line utility and JSON file. Doesn’t matter which method you choose, both will do the intended work and adjust your Node.js setup to the way you want it.

Next week, we’ll take a look back and recap all published posts within this PM2 series.


Additional Resources

Explore the Library

Find interesting tutorials and solutions for your problems.