26

I'm trying to deploy my working Windows 10 Spring-Boot/React app on Ubuntu 18.04 but keep getting "react-scripts: Permission denied" error despite numerous attempts to fix. Hopefully one of you react experts can spot what I'm doing wrong.

My package.json looks like this

{
  "name": "medaverter-front",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "bootstrap": "^4.4.1",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.0",
    "react-table-6": "^6.11.0",
    "react-validation": "^3.0.7",
    "reactstrap": "^6.5.0",
    "validator": "^12.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I'm logged in as root and used nvm to install node and lts. I installed nvm like this:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

then did this:

nvm install node
nvm use node
nvm install --lts
nvm use --lts

Then I cd to /var/lib/jenkins/workspace/MedAverter/medaverter-front and install node_modules like this:

npm install -g

and then change the permissions to 777 recursively, like this:

chmod -R 777 node_modules

I also changed all the /root/.nvm permissions to 777 recursively, like this:

chmod -R 777 /root/.nvm

I can get it build once using

npm run build

but then I run a "Build Now" from Jenkins and it fails with the same

[[1;34mINFO[m] Running 'npm run build' in /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] [[1;34mINFO[m] > medaverter-front@0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] > react-scripts build [[1;34mINFO[m] 
[[1;31mERROR[m] sh: 1: **react-scripts: Permission denied**
[[1;31mERROR[m] npm ERR! code ELIFECYCLE
[[1;31mERROR[m] npm ERR! errno 126
[[1;31mERROR[m] npm ERR! medaverter-front@0.1.0 build: `react-scripts build` 
[[1;31mERROR[m] npm ERR! Exit status 126

Then I cd to /var/lib/jenkins/workspace/MedAverter/medaverter-front and run

npm run build

And also get the same error again:

> root@ubuntu-s-1vcpu-1gb-nyc1-01:/var/lib/jenkins/workspace/MedAverter/medaverter-front#
> npm run build
> 
> > medaverter-front@0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
> > react-scripts build
> 
> sh: 1: **react-scripts: Permission denied** npm ERR! code ELIFECYCLE
> npm ERR! errno 126 npm ERR! medaverter-front@0.1.0 build:
> `react-scripts build` npm ERR! Exit status 126

I've literally spent days trying to figure this out. Suggestions?

RobC
  • 20,007
  • 20
  • 62
  • 73
user3217883
  • 1,026
  • 2
  • 26
  • 58
  • Check the ubuntu user permissions you run node with – R.LM Jun 01 '20 at 20:13
  • Why did you install globally and then change permissions in `node_modules`, a non-global directory? Did you try removing `-g`? Also, your chmod stuff isn't necessary, and is actually really dangerous. Please don't do that. Permission denied usually means the executable flag is not set, or it's chowned as another user and the bits aren't set for anyone other than user/group. The way you're installing modules is super weird though. – Qix - MONICA WAS MISTREATED Jun 03 '20 at 20:59
  • Yes, I tried it without the - g initially and couldn't get that to work. Added the -g later attempting different solutions. I know the final solution should not involve setting 777 privileges but I'm just trying to get something to work, then back off to the proper way later once the real solution gets revealed. Do you know what that is? – user3217883 Jun 04 '20 at 00:49
  • I had encountered similar issue when I installed npm and nvm with sudo. After uninstalling and installing it back with regular user permissions fixed it for me. – pritam Jun 04 '20 at 06:15

9 Answers9

67

Solution 1:

I think you have react-script globally installed. so try this command

npm install react-scripts --save

and then run the application again.

Solution 2:

try this command

sudo chmod +x node_modules/.bin/react-scripts

and then run the application again.

Solution 3;

I think your npm not have permission. you can try to run by sudo

sudo npm run build

and you can fix this problem like this

Step 1:

check path of npm if you are using npm by

which npm

you will "/usr/local/bin/npm" this type of path

OR

check path of yarn if you are using yarn by

which yarn

you will "/usr/local/bin/npm" this type of path

Step 2:

give permission 777 to this path and try to run project

sudo chmod -R 777 /usr/local/bin/npm
Muhammad Numan
  • 17,689
  • 4
  • 42
  • 65
  • root@ubuntu-s-1vcpu-1gb-nyc1-01:~# which npm /root/.npm-global/bin/npm chmod -R 777 .npm-global I also did the same for yarn but still getting same error. – user3217883 Jun 04 '20 at 15:37
  • @user3217883 try this npm install react-scripts --save and then run the app again – Muhammad Numan Jun 04 '20 at 15:41
  • @user3217883 I have updated my answer. you can try solution 1 and solution 2 – Muhammad Numan Jun 04 '20 at 15:45
  • 1
    yes that worked! Now I'm going to try a "Build Now" from Jenkins and see if it continues to work. – user3217883 Jun 04 '20 at 15:49
  • crap. now I'm getting some new error there. I don't think its related: FATAL: Could not checkout 07bc21e6a1ad6baea65f5cb921ac763c1fd89243 hudson.plugins.git.GitException: Command "git checkout -f 07bc21e6a1ad6baea65f5cb921ac763c1fd89243" returned status code 128: stdout: stderr: error: unable to unlink old 'medaverter-front/node_modules/@babel/code-frame/LICENSE': Permission denied – user3217883 Jun 04 '20 at 15:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215322/discussion-between-muhammad-numan-and-user3217883). – Muhammad Numan Jun 04 '20 at 16:08
  • stackoverflow.com/a/50319941/… can you try this – Muhammad Numan Jun 04 '20 at 16:24
  • If anybody else runs across this, I also had to do a chmod -R 777 node_modules after solution 1 to avoid error in Jenkins which was giving another permission denied error in a node_modules/@babel/code-frame/LICENSE file – user3217883 Jun 04 '20 at 17:20
  • @user3217883 rm -rf node_modules && npm install can you try this. I think due to permission issue there are other errors are occuring – Muhammad Numan Jun 04 '20 at 17:27
  • 2
    thanks, I tried solution 2, and it worked. – Jatinder Kumar Jul 28 '21 at 12:47
14

Solution for macOS

From the root of your project run:

chmod +x node_modules/.bin/react-scripts
Chris Perry
  • 3,162
  • 2
  • 6
  • 19
7

Instead of the default "start": "react-scripts start" specified in your package.json file, try changing it to the following and then try: "scripts": { "start": "node ./node_modules/react-scripts/bin/react-scripts.js start" }

Technext
  • 7,527
  • 7
  • 42
  • 69
  • Tried that but getting the same error. Do I need to close out the PuTTY session and start a new one after making the change? – user3217883 Jun 04 '20 at 15:42
4

I had modified the permissions on the folders earlier and undid the changes. I believe that is it.

I just deleted the node_modules folder and reinstalled the dependencies with the yarn command.

sudo rm -rf node_modules
yarn
yarn start
Samuel Terra
  • 539
  • 5
  • 4
0

It seems you don't have the right permission to build or execute the react app,

What you could do is try changing the owner to the current user using

   sudo chown -R username:username /var/lib/jenkins/workspace/MedAverter/medaverter-front/

you can check your username by typing whoami in the terminal this is done because the current user does not have access to to the var folder or anything inside it by default. Furthermore you want to change the access mode to read and execute for everyone using

 sudo chmod 755 /var/lib/jenkins/workspace/MedAverter/medaverter-front/

then you can want to change to root user using

sudo su

and then build the app using

npm run build

This has worked for me when i was facing somewhat a similar problem , hopefully it works for you as well,Cheers :)

Abdullah Abid
  • 1,293
  • 2
  • 8
  • 20
  • 1
    Thanks for the post. I didn't see it until after I had already posted my solution. There is only one user on the DigitalOcean server and that is root. Isn't su the same as root? – user3217883 Jun 08 '20 at 19:48
  • `Isn't su the same as root?` Not necessarily,without additional parameters,Yes: `su` would shift to root user.su is actually the system(which has root privileges) but since root has most privileges it should work in the first place, Since the server uses `www-data` as the default user, so anything that the `www-data` can access the server can access ,you could simply replace `username` by `www-data` and skip the 3rd command and it should be all good. Anyways it seems you already solved it so,Cheers :) – Abdullah Abid Jun 08 '20 at 21:59
0

I finally figured out a solution to this problem. It took days of effort. First, I deleted the Jenkins project and created a new one using Pipeline, rather than Freestyle. Then I added a Jenkinsfile with a script. That continued to fail but now I had the flexibility to add in additional commands to handle the errors.

One new error was something about jest-worker. I ran the following command on the DigitalOcean server to get past that one:

yarn add jest-worker

Then I had the old permissions error again. I think all these permission errors are due to user jenkins trying to run things owned by user root, even though everything had 777 permissions. I don't understand that but here's how I got around it. Modify the /etc/sudoers file and add the following line:

jenkins ALL=(ALL) NOPASSWD:ALL

Then modify the Jenkins file script to include recursive chmod and chown. Here is the full script that FINALLY worked:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                echo 'Checkout...'
                sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
                checkout scm
                sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
                sh 'sudo chown -R jenkins /var/lib/jenkins/workspace/MedAverter/medaverter-front'
                stash 'sources'
            }
        }
        stage('Build') {
            steps {
                echo 'Build...'
                unstash 'sources'
                sh 'sudo chmod -R 777 /var/lib/jenkins/workspace/MedAverter/medaverter-front'
                sh 'sudo chown -R jenkins /var/lib/jenkins/workspace/MedAverter/medaverter-front'
                sh 'mvn clean package -DskipTests'
                stash 'sources'
            }
        }
    }
}

I also had to increase the memory of the DigitalOcean droplet from 1GB to 2GB to get past another error. If someone knows a better way to get past that frustrating permissions error, please comment.

user3217883
  • 1,026
  • 2
  • 26
  • 58
0

That happens because of lack of a package.json file Just run this command and the issue will be fixed:

npm audit fix
0

In my case it was giving at npm run. this is how i solved.

  1. rm -rf /node_modules
  2. npm i
  3. npm run android
Ahsan Khan
  • 59
  • 2
-1

Sometimes npm need root permission. Try using sudo in the command. It resolved a similar issue for me. This might help.

sudo npm run build