Using PM2 with Heroku
This docs will guide you step by step through the PM2 integration with Heroku.
We will use Git and the Heroku CLI.
Prepare your app
Set your ecosystem file
Generate an ecosystem.config.js
template with:
pm2 init
Modify the ecosystem file to match your needs:
module.exports = {
apps : [{
name: "app",
script: "./app.js",
instances: "max",
env: {
NODE_ENV: "development",
},
env_production: {
NODE_ENV: "production",
}
}]
}
Learn more about ecosystem file here.
We recommend to use the cluster mode with Heroku, as each dyno has multi-core CPU.
Learn more about the cluster mode.
Add PM2 as a module
Add pm2 as a dependency to your projet.
With npm:
npm install pm2
With yarn:
yarn add pm2
Set your package.json
In your package.json
, modify your start
script like the following:
{
"scripts": {
"start": "pm2-runtime start ecosystem.config.js --env production"
}
}
Deploy with Heroku
Create an account on Heroku
Sign up for an account on Heroku here.
Install the CLI
Follow the instructions to install here.
Then, run heroku login
to connect the CLI to your account.
Init your Heroku app
We will first create a new empty application on Heroku and an associated empty Git repository.
Run this command from your app root folder:
heroku create
Creating app... done, ⬢ guarded-island-32432
https://guarded-island-32432.herokuapp.com/ | https://git.heroku.com/guarded-island-32432.git
You now have a new git remote named heroku
. If you push to this repository, your code is automatically deployed at the given URL.
Deploy your app on Heroku
Add and commit all your changes, then run:
git push heroku master
Initializing repository, done.
updating 'refs/heads/master'
remote: Compressing source files... done.
remote: Building source:
...
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/aqueous-temple-78487.git
You are ready
That’s all! The last line of the deployment will give you the URL where your app is available.
Next Steps
Complete your configuration with the Ecosystem File.
Monitor your app on a web dashboard, with PM2 Plus.