Every once in a while, I run into a frustrating issue where running yarn dev only works if I prefix it with sudo. That’s always a red flag — using sudo with Node, Yarn, or npm can lead to permission problems and broken setups later.
This post is a quick reference I can come back to whenever this happens on macOS.
The Problem
At some point, I’ve usually run a command like:
sudo yarn install
or installed Node/Yarn in a way that caused files to be owned by root.
When that happens, Yarn can’t read or write files unless I use sudo. The real fix is changing ownership, not elevating permissions.
Fixing Project Directory Permissions
On macOS, your default group is typically staff, not your username. So instead of user:user, this is the correct approach:
sudo chown -R your-username:staff /Users/your-username/path/to/your/project
Replace:
- your-username with your macOS username
- /path/to/your/project with the absolute path to your project directory
This command:
- Recursively (
-R) changes ownership - Sets the owner to my user
- Sets the group to
staff
After this, I can usually run:
yarn dev
without needing sudo.
Fixing Global Yarn and npm Permissions
If the problem persists, it’s often because Yarn or npm’s global directories are also owned by root.
These commands fix the most common locations:
sudo chown -R $USER ~/.yarn
sudo chown -R $USER ~/.config/yarn
sudo chown -R $USER ~/.npm
After running these, I restart my terminal and try again.
Why This Happens
- macOS uses
staffas the default group - Running
sudo yarnorsudo npmchanges ownership toroot - Later commands fail due to permission errors
Reclaiming ownership usually fixes everything.
Related Setup Notes
For a basic Node.js + TypeScript setup, I often refer back to this post:
Setting up TypeScript with Node.js (Basic Setup)
It’s a solid foundation before adding tools like Yarn, Vite, or Next.js.
Final Reminder
- ❌ Don’t run
sudo yarn - ❌ Don’t run
sudo npm - ✅ Fix ownership instead
- ✅ Consider using
nvmto avoid these issues entirely
Connect and Share
I'm always looking to learn from the community! If you have suggestions for improving this setup or experience with similar tools, I'd love to hear from you. Feel free to:
- Connect with me on LinkedIn
- Share your thoughts in the comments on the LinkedIn post
Remember, good tooling should enable productivity, not hinder it. This setup aims to provide a solid foundation that you can build upon for your specific needs.