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
n8nis globally installed usingnpmpm2is used to manage then8nbackground process- Terminal access with
sudoprivileges
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=trueflag 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
n8ndoesn’t affect your actual automation data. - If you’ve customized the storage path (via
.envorpm2config), 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 stopbefore upgrades - Keep
.envand project folders version-controlled if possible
Common Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
ENOTEMPTY | Folder rename failed | Rename folder manually |
EACCES | Permission denied | Use --unsafe-perm=true and sudo |
n8n: command not found | Missing global path | Ensure npm bin path is in $PATH |
| Workflows missing | Wrong data folder | Check 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!