This is my little cheatsheet for fixing the classic:
Error: EACCES: permission denied, mkdir when installing an npm package without sudo.
Installing it globally with sudo npm i -g looks off to me.
Why this happens
On many Linux setups, global npm installs default to a system directory
that your user can't write to. So npm tries to mkdir somewhere it shouldn't, and you get EACCES.
The quick fix (My preferred way)
Create a user owned folder for global installs, point npm to it, and add it to your PATH:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profileThis will make a directory .npm-global for global installations.
Then npm is configured to use new directory path for global installations.
Then that PATH is exported, so you can call any globally installed module from terminal.
Then system variables are updated. You could have also logged out and logged back in, but this is faster.
Test it
Download a package globally without using sudo:
npm install -g <somepackage>Make it permanent
To make the PATH export more permanent, edit `.profile` file and add:
export PATH=~/.npm-global/bin:$PATHAnd do source .profile once again.
If you happen to have to source .profile on each login, check that your .bashrc contains:
if [ -f ~/.profile ]; then
source ~/.profile
fi
That's it. Btw, while I'm at sudo stuff, I have a post about recovering from a bad sudo memory. For your next read, if you're in npm land anyway, read about private/public registries pain.
