57

I am trying to run an Angular application. I execute the ng serve command, but it then shows:

Error: Cannot find module 'node-sass'

2

12 Answers 12

140

Solution:

You need to change the ownership of folder node_modules, because you use sudo npm install -g node-sass, so its ownership is set to root.

To change the ownership to the current user as group -

sudo chown -R root:$(whoami) /usr/local/lib/node_modules/

sudo chmod -R 775 /usr/local/lib/node_modules/

And never use sudo for an npm-related query.

7
  • 12
    worked for me but by not putting root: ahead of username in first command
    – nirmal
    Jul 2, 2019 at 2:50
  • 1
    Instead of the first command use this sudo chown -R root:YOUR_USERNAME /usr/local/lib/node_modules/ Jun 18, 2020 at 4:14
  • 1
    after that i got one more error: checkPermissions Missing write access to /usr/local/lib/node_modules but this answer was helpful stackoverflow.com/a/40905762/9277453 Nov 20, 2020 at 21:39
  • 6
    on mac OS : sudo chown -R $(whoami):staff /usr/local/lib/node_modules/
    – benek
    Aug 14, 2022 at 16:46
  • 2
    although it should work, it's a bad idea to change permissions there. Please refer to this answer so that is not needed : stackoverflow.com/questions/33725639/… Feb 27 at 20:33
60

Run this command

sudo npm install -g <ModuleName> --unsafe-perm=true --allow-root
2
  • 6
    This worked for me. Any explanation would be helpful.
    – A_01
    Feb 26, 2021 at 10:49
  • 1
    An explanation would be in order. E.g., what is the idea/gist? What are the security implications of it? How does it work? Why does it work? From the Help Center: "...always explain why the solution you're presenting is appropriate and how it works". Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Jan 25, 2022 at 15:22
26

I was working in Ionic and had this issue, so I solved this by moving one folder back and running this command:

sudo chmod -R 777 project-directory

And after this:

npm install node-sass --save

22

Try this

sudo npm install -g --unsafe-perm node-sass

Or this

sudo npm install -g --unsafe-perm --verbose @angular/cli

1
  • 1
    I have a machine using root user to run npm install and still failed. Only --unsafe-perm does work.
    – Louis Go
    Sep 7, 2021 at 8:12
11

This worked for me -

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
1
  • 1
    On a brand new M1 mac this was the only answer that worked for me.
    – Luke
    Apr 11 at 22:22
6

This is due to the user's permission. The user from which you are running the command of npm install is probably doesn't have permission. So you are getting errors.

For that user just give the permission for that project folder. In Mac, use the below steps and give read and write permission to the user for that project folder

  • On your Mac, select a disk, folder, or file, and then choose menu FileGet Info.

  • If the information in Sharing & Permissions isn’t visible, click the arrow.

  • If the lock at the bottom right is locked, click it to unlock the Get Info options, and then enter an administrator name and password.

  • Click a user or group in the Name column, and then choose a privilege setting from the pop-up menu.

    • Read & Write: Allows a user to open the item and change it.

    • Read-only: This allows a user to open the item but not change its contents.

    • Write only (Drop Box): Makes a folder into a drop box. Users can copy items to the drop box, but can’t open it. Only the owner of the dropbox can open it.

    • No Access: Blocks all access to the item.

This helped me to solve the problem.

1
  • Yes, this is the kind of answers we want, not "Try this" or "This worked for me" answers without an explanation and with unspecified security implications. Jan 25, 2022 at 15:38
1

Try this: npm install node-sass@version

If you want the latest, then just try npm install node-sass.

If you're getting an access problem then:

  1. Windows: command prompt in administrator mode, and then run the above.

  2. Mac:

     sudo npm install node-sass
    

References

1

2023 Working Solution

npm i --unsafe-perm node-sass

if node_modules is owned by root, then do

sudo su //switched to root user
npm i --unsafe-perm node-sass

Reference: https://github.com/sass/node-sass/issues/2824#issuecomment-575960022

1

Run these commands on your terminal:

sudo chown -R $USER /usr/local/lib/node_modules/
sudo chown -R $USER /usr/local/bin/
sudo chown -R $USER /usr/local/share/
0

If you have not added Node.js pakage.json yet, try running node init. If you already have this, npm install node-sass.

If both are present and you are using Ubuntu, try changing the permission of the folder using chmod -R <path to node module>.

0

Make sure you are not in the home directory or other places. You need to go back to root.

Use this command

cd ..
cd ~

Your current location should look like:

~$

After this, run the command to create a project.

1
  • 1
    Why would cd .. make any difference? Jan 25, 2022 at 15:37
0

u need to use a "root" user in your shell,

with this command:

sudo su -

then you should enter your password

and then try to install the package, i think it will work with any package that you try to install globally

more info that help me in this website

http://www.dark-hamster.com/programming/how-to-solve-error-message-eacces-permission-denied-in-nodejs-application/

1
  • As stated in some of the other answers, running npm commands as root should be avoided whenever possible.
    – ahuemmer
    Jun 27, 2022 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.