Search code examples
reactjsubuntunpmcreate-react-appsudo

How to stop permission when creating a new react app


I am using an ubuntu os and each time I try to create a new react app without adding sudo, I get this error:

Creating a new React app in /home/bunny/bunny.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts with cra-template...

npm ERR! Unexpected end of JSON input while parsing near '...jUJStbKRw4Wj3LhB5a/JP'

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bunny/.npm/_logs/2020-12-31T13_36_25_597Z-debug.log

Aborting installation.
  npm install --save --save-exact --loglevel error react react-dom react-scripts cra-template has failed.

Deleting generated file... package.json
Deleting bunny/ from /home/bunny
Done.

If I use sudo in creating the react app successfully, I would have to input my password everytime I want to save a changes made in a file, add a new folder or file or even delete files and I can only add new files from the terminal.

I have searched for a solution to this and the last thing I tried was changing the owner of the directory so I could modify or create an app without the sudo priviledges and I did this:

sudo chown -R bunny:staff '/home/bunny/.npm/'

This doesn't seem to work either. I need help because inputing a password everytime and creating files from the terminal is now frustrating


Solution

  • There are two possibilities for this to fail.

    First: You do not have npx or npm installed under your current user and it is rather installed to the root user (which is what sudo does - elevates you to a root user).

    Second: You do not own the directory /home/bunny/bunny/.

    Now, if you have the first issue then that could cause the second issue too as it would create the project directory under root user, which means you will not be able to create, delete files without sudo privileges. This seems highly likely because you cannot run npx create-react-app in the first place. So try running,

    sudo chown -R $(whoami) ~/.npm
    

    If you still face issues, I have found a similar question which has been answered - check it out here.