2

I am new to Linux and I am trying to get set-up with nodejs, but I have not been able to figure out why I can't install react. I am following directions that I have seen on the web, but I am wondering why I seem to not have the privileges necessary to install react. It's a personal environment, so I wasn't expecting any issue. Below is what I have run and the final error that the last line gave me.

sudo apt update && sudo apt upgrade
sudo apt install nodejs
sudo npm install npm

node -v
v8.10.0

npm -v
3.5.2

sudo npm install npm@latest -g
npm -v
6.10.0

npm install -g create-react-app

Everything above runs just fine until the last command. Then I get the following error, regardless of working directory.

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/me/.npm/_logs/2019-07-06T19_33_32_971Z-debug.log
jhan
  • 41
  • 5

2 Answers2

0

Using npx (with npm 5.2+)

If you're using npm 5.2+, you may create a ReactJS app like below:

npx create-react-app my-app

Now your app is in a folder my-app, you can cd into the folder and run npm start.

Using older versions of npm with npm install

You may also install it with a global install with npm, infact for older npm versions (< 5.2) the official documentation for create-react-app links to instructions by Dan Abramov (@co-author of create-react-app):

npm install -g create-react-app

However you may need sudo for a global -g install or work around it

Everything above runs just fine until the last command. Then I get the following error, regardless of working directory.

That's because your last command is a global install (-g).

Unless there is a reason against, you may install it with sudo:

sudo npm install -g create-react-app

To install without sudo, please refer to this question: npm throws error without sudo

bakkal
  • 54,350
  • 12
  • 131
  • 107
  • I have been told that it is improper to install node / npm / react with sudo as that creates permission issues. Is this true? How should one get around that issue? – jhan Jul 17 '19 at 20:52
  • 1
    @jhan Since you're using `npm 6.10`, please try the `npx` way at the top of the answer as an alternative. – bakkal Jul 17 '19 at 21:37
  • 1
    @jhan I saw your other question, the problem is not installing with `sudo npm install -g create-react-app`. The problem is because you created the app with `sudo` with `sudo create-react-app my_project`, you don't have to do that, you can install it with your current user without `sudo` so `create-react-app my_project`. – bakkal Jul 17 '19 at 21:43
0

I was also having this issue, here is how it was resolved

Make sure you have npm installed or use:

sudo apt install npm

Then

sudo npm -g install create-react-app

To create the app I did not use 'npm' before the command line, instead use this:

create-react-app nameofyourapp

That worked for me.

Snowcat
  • 470
  • 8
  • 16