JavaScript is a programming language every programmer and developer has been using extensively for building interactive websites and apps. Working on JavaScript is so smooth for programmers who know all the features, functions, and classes. It has many libraries that make the programming simpler. The ease of programming is achieved when using a library, and React.js is one of the libraries that are widely used. It is easier to build an interactive user interface with this open-source library. React.js is also known as a flexible, declarative, and efficient JavaScript library. When working on react, you may experience the error “sh: react-scripts: command not found”.
The error always seems irritating whenever it pops up, and it keeps you wondering how to get it resolved. Well, we are here to give you a helping hand to drag you out of the error by providing valuable solutions. Before skipping to the solution section, you need to know how the error shows up. Check it out
How do you get the error?
When you try to install packages
npm install -g create-react-app
npm install --save react react-dom
and then run the ‘npm command’, you get the error message. Have look at the error you get into
sh: react-scripts: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! [email protected] start: `react-scripts start`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/itsjavascript/.npm/_logs/2022-07-09T11_50_07_325Z-debug.log
That’s how you get the error.
Cause of the error
Due to multiple reasons, you get the error message. It happens
- If you run the project and create a clone with no package dependencies.
- If no package is installed.
- In the case you try to use npm install, you get the error if your project is initialized with yarn. Npm and yarn are incompatible with each other.
Ways to Fix the Error “sh: react-scripts: command not found”
We have a few options to solve the error in an efficient way.
Option 1 – Replace the package.json file
To solve the error, you need to replace a command line once you access a package.json file.
Replace this command
"start": "react-scripts start"
By the following command
"start": "NODE_ENV=production node_modules/react-scripts/bin/react-scripts.js start"
Option 2 – Delete the node_modules
Sometimes deleting the node-modules can fix the error. Use the below codes
# delete the node_modules
rm -rf node_modules
# delete the package-lock.json or yarn.lock
rm -f package-lock.json
rm -f yarn.lock
# clean the npm cache
npm cache clean --force
# Install the dependencies
npm install
You can use the above commands to fix the error.
Option 3 – Set up react-script
To set up the react-script, you need to run a command before starting your program using npm start. Check out the command
npm i react-scripts
It resolves the error.
Conclusion
Here we shed light on the solutions to remove the error “sh: react-scripts: command not found”. You can simply implement the option that fits your project requirement. I hope you find it helpful!