Home / Configs / Servers / PM2 ecosystem.config.js

PM2 ecosystem.config.js example: run Node apps in the background

Run a Node.js app with PM2: restarts on crash, memory limit, cluster mode and start at boot.

For Node.js apps managed with PM2 on Linux, macOS or Windows.

PM2 keeps a Node.js app running, restarts it if it crashes, and can start it at boot. An ecosystem file keeps all its settings in one place. Fill in your app below and save the result in your project folder.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Relative to the folder, or a full path
Examples: 200M, 1G

ecosystem.config.js
module.exports = {
  apps: [
    {
      name: "myapp",
      script: "server.js",
      instances: 1,
      exec_mode: "fork",
      watch: false,
      autorestart: true,
      max_memory_restart: "300M",
      env: {
        NODE_ENV: "production",
        PORT: 3000,
      },
    },
  ],
};

How this setup works

Diagram: ecosystem.config.js, then PM2, then Your app
ecosystem.config.js (your settings) → PM2 (starts and watches the app) → Your app (restarts if it crashes)

Step-by-step setup

  1. Install PM2: npm install -g pm2.
  2. Save the file as ecosystem.config.js in your project folder.
  3. Start the app: pm2 start ecosystem.config.js.
  4. Check it: pm2 status and pm2 logs myapp.
  5. Start it at boot: run pm2 startup, run the command it prints, then pm2 save.

What the important lines mean

Common errors and how to fix them

Script not found
The path is wrong or relative to a different folder. Use a full path or set the working folder.
The app is stuck in an errored state
Run pm2 logs myapp to see the error. Often a missing module or a port already in use.
The app is gone after a reboot
Run pm2 startup, then the command it prints, then pm2 save.
Cluster mode does not start
Cluster mode suits servers that listen on a port. For scripts and workers, use fork mode.

Frequently asked questions

What is the difference between fork and cluster mode?
Fork runs one copy. Cluster runs several copies of a web server that share one port, using all CPU cores.
How do I reload the app without downtime?
In cluster mode, run pm2 reload with the app name.
Where do the logs go?
PM2 keeps them in ~/.pm2/logs. Use pm2 logs to read them.

Written for: PM2 5.4, Node 20 · Updated 2026-10-03

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators