Fixing macOS Bun / NPM / Yarn AccessDenied Error Permanently
System-level solution for 'unable to write to tempdir: AccessDenied' error in package managers like Bun, NPM, and Yarn on macOS.
Fixing macOS Bun / NPM / Yarn "AccessDenied: unable to write to tempdir" Error Permanently
If you're getting this error when running bun install, npm install, or yarn install on macOS:
error: bun is unable to write files to tempdir: AccessDenied
You're not alone! This error is caused by corrupted permissions in macOS system temporary directories (/tmp, /private/tmp, or $TMPDIR). As a result, CLI tools get denied when they try to create temporary files.
Symptoms
If all package managers (bun, npm, yarn, pnpm) are failing in the same way, the root cause is most likely macOS's $TMPDIR environment variable pointing to an invalid directory.
Solution: Repair and Fix Temp Directories
The following one-liner command fixes system temp permissions, creates a user-specific ~/.tmp directory, and adds a permanent $TMPDIR to your shell config:
sudo chown root:wheel /tmp /private/tmp && \
sudo chmod 1777 /tmp /private/tmp && \
mkdir -p "$HOME/.tmp" && chmod 700 "$HOME/.tmp" && \
sed -i '' '/TMPDIR=/d' "$HOME/.zshrc" 2>/dev/null || true; \
printf '\n# Force user-level tmp to avoid macOS TMPDIR quirks\nexport TMPDIR="$HOME/.tmp"\n' >> "$HOME/.zshrc"; \
source "$HOME/.zshrc"; \
echo "TMPDIR -> $TMPDIR"; \
ls -ld /tmp /private/tmp "$HOME/.tmp"; \
mktemp || true
What the Command Does
| Step | Description |
|---|---|
sudo chown root:wheel /tmp /private/tmp | Fixes ownership and group of system temp folders to root:wheel |
sudo chmod 1777 /tmp /private/tmp | Sets permissions so everyone can write but can't delete others' files (sticky bit) |
mkdir -p "$HOME/.tmp" | Creates a user-specific temp folder |
chmod 700 "$HOME/.tmp" | Makes it secure so only you can read/write |
sed -i '' '/TMPDIR=/d' ~/.zshrc | Removes old TMPDIR definitions from .zshrc |
export TMPDIR="$HOME/.tmp" | Sets temp directory to ~/.tmp for every terminal session |
mktemp | Final check: tests if it's actually writable |
Verification
Open a new terminal and run these commands:
echo $TMPDIR
ls -ld $TMPDIR
Expected output:
/Users/<username>/.tmp
drwx------ ...
Now bun install, npm install, yarn install will all work normally. The problem is completely resolved.
Why Does This Happen?
macOS creates random temp paths like /var/folders/.../T/ for each user session. However, sometimes after a reboot or update, this folder gets deleted, but the $TMPDIR environment variable still points to the old path. When CLI tools try to write there, they get an "AccessDenied" error.
With the ~/.tmp solution, you now have a permanent temp folder under your control, and macOS's random directory management won't affect you.
Security Note
Thanks to chmod 700 ~/.tmp, only you can read/write to it. It's secure even on multi-user systems. Additionally, even if system updates break /tmp permissions, your $TMPDIR remains unaffected.
Summary
| What's Done | Why It's Done |
|---|---|
/tmp permissions repaired | System-wide temporary files work properly |
~/.tmp created | User-specific secure temp folder |
$TMPDIR fixed | CLI tools work consistently |
Result
After these steps, all tools like bun, npm, yarn, pnpm, vite, eslint will work seamlessly. You'll never see the "unable to write files to tempdir: AccessDenied" error again.
© 2025 Gökhan Öztürk – macOS Developer Tips