Build script returned non-zero exit code: 2 - react-scripts

Hi Netlify community!

I need your help with something…
I’m currently facing a blockage because my build is failing. The error message indicates that I don’t have react-scripts installed; however, I have confirmed its presence by running npm list react-scripts, which shows react-scripts@5.0.1.

Here’s a snippet from my package.json:

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
},
"devDependencies": {
    "@types/styled-components": "^5.1.34",
    "react-scripts": "^5.0.1",
    "rimraf": "^6.0.1",
    "web-vitals": "^4.2.4"
}

I’ve also attempted to reinstall react-scripts, clear the cache, and reinstall all dependencies, but I continue to encounter the same errors.
Also, npm run build is working fine locally.

Below is the log for the failing build:

[...]
3:45:12 PM: > react-scripts build
3:45:12 PM: sh: 1: react-scripts: not found
3:45:12 PM: ​
3:45:12 PM: "build.command" failed                                        
3:45:12 PM: ────────────────────────────────────────────────────────────────
3:45:12 PM: ​
3:45:12 PM:   Error message
3:45:12 PM:   Command failed with exit code 127: npm run build (https://ntl.fyi/exit-code-127)
3:45:12 PM: ​
3:45:12 PM:   Error location
3:45:12 PM:   In Build command from Netlify app:
3:45:12 PM:   npm run build
3:45:12 PM: ​
3:45:12 PM:   Resolved config
3:45:12 PM:   build:
3:45:12 PM:     command: npm run build
3:45:12 PM:     commandOrigin: ui
3:45:12 PM:     publish: /opt/build/repo/build
3:45:12 PM:     publishOrigin: ui
3:45:13 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
3:45:13 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
3:45:13 PM: Failing build: Failed to build site
3:45:13 PM: Finished processing build request in 7m59.545s

The repo: https://github.com/julianoiret/mylittlejournal

I would greatly appreciate any assistance you can provide to help resolve this issue. Thank you!

Julia

@jujudefruit This may not be the case for you, as I can’t tell from your build log snippet, but check the warning in this box:

Dependencies and production

If you set the NODE_ENV to production, any devDependencies in your package.json file will not be installed for the build.

Thanks for the reply @nathanmartin.

I’ve moved react-scripts to my dependencies and removed it from the devDependencies, but I’m still encountering the same error.
Apologies in advance if I’m slow to respond; I’ll be able to follow up in a few days. It’s frustrating not to have deployed this before my leave!

I forked your repo and I am unable to get past the dependency installation stage due to this bug: [BUG] npm ERR! Exit handler never called! · Issue #3577 · npm/cli. I see this error in your build logs as well, so it seems like NPM is causing some issues. Maybe one or more of your dependencies is an issue.

Thank you all so much for your help!
I finally managed to publish my website. I deleted the package-lock.json file, which is a solution I don’t particularly like, but it’s a temporary fix.