Permission Issues When Installing node-sass via NPM

I’m having trouble installing node-sass on my Linux Mint machine. I used sudo npm install node-sass but got some weird errors about access to directories. This doesn’t make sense since I used sudo.

I’ve tried a bunch of stuff from online forums like removing and reinstalling node, trying different versions, but nothing works. Here’s what the error looks like:

> [email protected] setup /home/user/projects/my-website/node_modules/scss-compiler
> node scripts/setup.js

Can't save binary /home/user/projects/my-website/node_modules/scss-compiler/bin/linux-x64-57 : { Error: EACCES: permission denied, mkdir '/home/user/projects/my-website/node_modules/scss-compiler/bin'
    at Object.fs.mkdirSync (fs.js:885:18)
    at sync (/home/user/projects/my-website/node_modules/make-dir/index.js:71:13)
    at Function.sync (/home/user/projects/my-website/node_modules/make-dir/index.js:77:24)
    at getBinaryAndSave (/home/user/projects/my-website/node_modules/scss-compiler/scripts/setup.js:114:11)
    at Object.<anonymous> (/home/user/projects/my-website/node_modules/scss-compiler/scripts/setup.js:157:1)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/home/user/projects/my-website/node_modules/scss-compiler/bin' }

Any ideas what’s going on? I’m stumped!

I’ve encountered similar issues before, and it can be frustrating. One thing that worked for me was avoiding using sudo with npm altogether. Instead, try setting up a local npm prefix in your home directory. You can do this by running:

npm config set prefix ~/.npm-global

Then add ~/.npm-global/bin to your PATH in your .bashrc or .zshrc file. After that, you can install packages globally without sudo:

npm install -g node-sass

This approach keeps everything contained in your user space, avoiding permission conflicts. It’s generally considered a safer practice too. If you’re still having trouble after trying this, you might want to check your node and npm versions to ensure they’re compatible with the node-sass version you’re trying to install.

ugh, hate those permission issues. have u tried running npm with the --unsafe-perm flag? like sudo npm install node-sass --unsafe-perm? sometimes that works for me when i get those annoying access errors. also, make sure ur not accidentally running stuff as root in ur project folder, that can mess things up too

Have you considered using a Node Version Manager (NVM)? It’s a game-changer for handling these types of issues. NVM allows you to install and manage multiple Node.js versions without needing sudo privileges. Here’s what I’d suggest:

  1. Install NVM
  2. Use NVM to install the Node version you need
  3. Install node-sass locally in your project

This approach bypasses system-wide installations and keeps everything contained within your project. It’s solved similar problems for me in the past, and it makes switching between Node versions for different projects much easier. If issues persist, consider checking your project’s package.json for dependency conflicts.