How to Safely Update n8n Installed via npm with pm2 (Without Losing Data)

If you’re running n8n globally via npm and managing it with pm2, updating it might occasionally throw errors like:

npm ERR! ENOTEMPTY: directory not empty, rename '/usr/lib/node_modules/n8n' -> '/usr/lib/node_modules/.n8n-xxxxx'

This guide will walk you through safely updating n8n without losing any data, even when encountering common npm issues.


Prerequisites

  • n8n is globally installed using npm
  • pm2 is used to manage the n8n background process
  • Terminal access with sudo privileges

Step-by-Step Guide

Step 1: Stop n8n from pm2

To prevent file locks or corruption during update:

pm2 stop n8n

Step 2: Rename the Existing n8n Folder

To avoid ENOTEMPTY or permission errors:

sudo mv /usr/lib/node_modules/n8n /usr/lib/node_modules/n8n_backup

This preserves the original files without deleting them and clears the way for a clean install.


Step 3: Reinstall the Latest Version of n8n

sudo npm install -g n8n --unsafe-perm=true

The --unsafe-perm=true flag ensures scripts can run correctly as root.


Step 4: Restart n8n with pm2

pm2 restart n8n

This will launch the newly installed version.


Step 5: Verify the Update

n8n --version
pm2 logs n8n

Confirm the version is updated and logs are clean.


What About My Workflows?

  • By default, your workflows and credentials are stored in ~/.n8n, not in the install folder.
  • Reinstalling n8n doesn’t affect your actual automation data.
  • If you’ve customized the storage path (via .env or pm2 config), double-check that location.

Optional: Clean Up the Backup

Once you’re confident everything works:

sudo rm -rf /usr/lib/node_modules/n8n_backup

Bonus Tips

  • For easier upgrades, consider switching to Docker or n8n Desktop
  • Always stop services with pm2 stop before upgrades
  • Keep .env and project folders version-controlled if possible

Common Errors and Fixes

ErrorCauseFix
ENOTEMPTYFolder rename failedRename folder manually
EACCESPermission deniedUse --unsafe-perm=true and sudo
n8n: command not foundMissing global pathEnsure npm bin path is in $PATH
Workflows missingWrong data folderCheck if you’re using a custom path

Updating n8n this way ensures that your services stay running smoothly and safely—without breaking your automations or losing data.

Let us know if you want to automate this process using n8n itself or a shell script!

Leave a Comment