Encountering EACCES permission issues during npm install -g less

I’m trying to install the Less CSS preprocessor in my development environment so that I can automatically transform .less files into .css files upon saving. I’ve already set up Node.js on my system. When I execute the command to install Less globally:

npm install -g less

I receive several permission denied errors, including:

☁  ~  npm install -g less
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/mkdirp/0.5.1'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/graceful-fs/3.0.8'
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules

npm ERR! Darwin 15.0.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "less"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES

I’m not very experienced with this setup and would love some advice on how to resolve these errors!

Had this exact issue at work. npm tries to install global packages in /usr/local/lib/node_modules by default, which needs root access. Sure, sudo works, but you’ll get ownership problems where root owns files instead of you. Use nvm instead - it puts node and npm in your home directory, so global installs just work. Install nvm, reinstall node through it, and npm install -g less will work without any permission headaches.

had the same issue last week! try sudo npm install -g less - sudo gives you admin rights to write to system folders. worked perfectly on my mac

Those permission errors happen all the time when npm tries writing to system directories. Sure, sudo works, but here’s a better fix - change npm’s default directory so you don’t need admin rights every time. Just run mkdir ~/.npm-global then npm config set prefix '~/.npm-global'. Add export PATH=~/.npm-global/bin:$PATH to your .bash_profile or .zshrc file. Now you can install global packages without permission headaches, and it’s way safer than constantly using sudo.