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'

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajeev Varshney
  • 933
  • 2
  • 13
  • 26
  • Use npm version 5.2.0. For more details go to https://stackoverflow.com/questions/48147896/error-in-cannot-find-module-node-sass/52982654#52982654 – Paras Agarwal Oct 25 '18 at 16:55
  • Follow this :- https://stackoverflow.com/questions/48147896/error-in-cannot-find-module-node-sass/52982654#52982654 – Paras Agarwal Oct 25 '18 at 16:57

12 Answers12

138

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anirban Sanyal
  • 1,420
  • 1
  • 7
  • 2
  • 12
    worked for me but by not putting root: ahead of username in first command – nirmal Jul 02 '19 at 02:50
  • 1
    Instead of the first command use this ```sudo chown -R root:YOUR_USERNAME /usr/local/lib/node_modules/``` – Jibin Thomas Jun 18 '20 at 04:14
  • 1
    after that i got one more error: ```checkPermissions Missing write access to /usr/local/lib/node_modules``` but this answer was helpful https://stackoverflow.com/a/40905762/9277453 – Alexey Nikonov Nov 20 '20 at 21:39
  • 5
    on mac OS : sudo chown -R $(whoami):staff /usr/local/lib/node_modules/ – benek Aug 14 '22 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 : https://stackoverflow.com/questions/33725639/npm-install-g-less-does-not-work-eacces-permission-denied/40905762#40905762 – Vincent Gerris Feb 27 '23 at 20:33
  • Thanks @benek.. Your response worked for me in MacOS – Roy M J May 23 '23 at 07:09
  • `sudo chown -R : /usr/local/lib/node_modules/` also works – digiwand Jul 29 '23 at 21:42
60

Run this command

sudo npm install -g <ModuleName> --unsafe-perm=true --allow-root
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
am2505
  • 2,194
  • 15
  • 19
  • 6
    This worked for me. Any explanation would be helpful. – A_01 Feb 26 '21 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](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/57777965/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 25 '22 at 15:22
25

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Taimoor Tariq
  • 405
  • 5
  • 5
22

Try this

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

Or this

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

swati kiran
  • 675
  • 1
  • 4
  • 22
  • 1
    I have a machine using root user to run npm install and still failed. Only `--unsafe-perm` does work. – Louis Go Sep 07 '21 at 08:12
11

This worked for me -

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
Mohan Dere
  • 4,497
  • 1
  • 25
  • 21
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ankit Gairola
  • 61
  • 1
  • 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. – Peter Mortensen Jan 25 '22 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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhruv Singh
  • 2,185
  • 2
  • 11
  • 17
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

habib
  • 1,454
  • 17
  • 31
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/
Taady
  • 2,409
  • 2
  • 10
  • 24
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>.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Santhosh S Kashyap
  • 973
  • 2
  • 13
  • 19
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
azhar
  • 351
  • 3
  • 13
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/

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