AI and Automation
How to Safely Update n8n Installed via npm with pm2 (Without Losing Data)
By Amjid Ali · · Updated · 2 min read
In this article
Prefer to watch? This video walks through it step by step. Prefer to read? The full guide is below.
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!
Related videos
Video library- AI and Automation
How to Update n8n Cloud to the Latest Version (Step-by-Step)
· 2:15 · 2.7K views
Watch on YouTube - Cloud and Self-Hosting
Self-Host n8n on Ubuntu with npm: Step-by-Step Guide
· 17:50 · 15.8K views
Read the guide Watch on YouTube
Keep reading
AI and AutomationWhat Is an AI Mattress? Benefits, Evidence, and How to Choose OneLearn how an AI mattress uses built-in sensors and automatic support to improve sleep comfort, with CAICT data. Explore options today. · 15 min read
AI and AutomationHow to Build an AI Model: A Complete Guide for DevelopersArtificial intelligence isn't just a buzzword anymore—it's become an essential tool for modern businesses. Whether you're looking to automate customer... · 6 min read
AI and AutomationThe Ultimate Guide: 20 Best Low-Code Platforms for Homelab and IT Automation ProjectsThe best low-code platforms for homelab projects and IT automation. Compare n8n, Node-RED, Appsmith, and 17 more tools — with pros, cons, and use cases. · 2 min read