Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu create-react-app fails with permission denied

I'm getting a weird error:

Unhandled rejection Error: EACCES: permission denied, mkdir '/home/ubuntu/.npm/_cacache/index-v5/14/36'atus

I just install npm (6.4.1) and node (11.2.0) on an AWS instance without problems. I installed create-react-app globally. The error says This is an error with npm itself.

I'm kind of at a loss. I created the directory /home/ubuntu/.npm/_cacache/index-v5/14 and it still wouldn't succeed. I obviously own and have write permissions in /home/ubuntu.

It looks like it succeeds with sudo. why ?

Edit: ubuntu:ubuntu owns the current and parent directory (I'm in /home/ubuntu/workspace)

like image 586
xyious Avatar asked Nov 24 '18 06:11

xyious


People also ask

Why my React app is not creating?

If, after all of the above, you are unable to run npx create-react-app my-app , then it might be an issue with Node itself. If you really need create-react-app , you might need to reinstall Node and reconfigure your dependencies to ensure you have a fresh start with Node, npm, npx, and the like.


2 Answers

TL;TR

Run:

sudo chown -R $USER:$USER '/home/REPLACE_WITH_YOUR_USERNAME/.npm/'

On Linux OS NPM and NodeJS are installed globally with sudo and the owner of that files is the root and usually a user can only read/execute that packages. When NPM is stalled a ~/.npm/ folder is created by the root. By running create-react-app you are executing the command as user and create-react-app is trying to modify something in the ~/.npm/ directory which is owned by the root and not to current user. You need to change the owner of that directory to you, so you can modify it without sudo privileges.

Often similar thing happens when you install NPM package with sudo e.g. sudo npm install <package> --save. Again the newly installed package in owned by the root and for example when you try to update/modufy/delete your project without sudo infrnt of NPM you will have similar permission error. In these cases navigate to your project directory and change its owner by running:

sudo chown -R $USER:$USER .
like image 141
Nedko Dimitrov Avatar answered Sep 22 '22 13:09

Nedko Dimitrov


New way of installation will resolve the issue.

According to the latest react documentation follow below steps to create react app

npx create-react-app my-app
cd my-app
npm start

Note(from ReactJS Team): If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app to ensure that npx always uses the latest version.

Refer official documentation: https://facebook.github.io/create-react-app/docs/getting-started

like image 44
Vineeth Bhaskaran Avatar answered Sep 21 '22 13:09

Vineeth Bhaskaran