Search code examples
node.jspm2

How to restart a node app via pm2 and apply new config file?


I have a node app running on Ubuntu 18.04. It was started using PM2 like so pm2 start ./bin/web_server.js

Under some circumstances the process runs out of memory throwing this error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xa18150 node::Abort() [node /path/to/app/bin/web_server.js]
 2: 0xa1855c node::OnFatalError(char const*, char const*) [node /path/to/app/bin/web_server.js]
...
...

So I want to increase the heap size, which seems to be pretty straitforward for a node process. All one has to do is run it with the proper parameter set. e.g. node --max-old-space-size=2048 ./bin/web_server.js

Since I am already running my process using PM2, I want to pass the max-old-space-size parameter to node using PM2's ecosystem.config.js file. So I proceeded to add this config file into the APP's root directory. The contents are here:

module.exports = {
    apps : [{
        name: 'IntuListAPI',
        script: './bin/web_server.js',
        //watch: true,
        //watch_delay: 1000,
        max_memory_restart: '20G',
        node_args: [
            "--max-old-space-size=6144"
        ]
    }]
};

Now when I run pm2 restart web_server I expect PM2 to pick up the new config and restart my process with it. But it does not seem to work and I can't figure out why. Since it was originally started without ecosystem.config.js, is it now ignoring it?

To be sure, the process restarts just fine, it just doesn't restart with the new heap size.


Solution

  • After some further research, I learned that in order to accomplish this I had to delete my APP from pm2 and then restart it using the ecosystem.config.js file. These two commands is all I needed:

    pm2 delete web_server
    pm2 start ecosystem.config.js 
    

    The last command will start up ./bin/web_server.js and apply all the specified parameters.