node.jsreactjsjenkinsnpm

"npm run build" = "react-scripts: Permission denied"


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?


Solution

  • 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.