[Solved] react-scripts: command not found

[ad_1]

If you are trying to run the react project using the npm start command on your terminal, you might have got an error stating react-scripts: command not found. The error usually occurs when you are cloning the project for the first time and running it.

In this tutorial, we will look at what is react-scripts: command not found and how to resolve this error.

What is react-scripts: command not found?

The react-scripts: command not found error occurs for multiple reasons.

  • If you clone the react project and run it without installing the package dependencies.
  • If the project is initialized with yarn and you attempt to use npm install. Yarn and npm are not compatible with each other. If we run npm install over yarn, it may even corrupt the tree.
  • The react-scripts package is not installed.

The detailed error log will look as shown below.

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

How to fix react-scripts: command not found?

Based on the issues we described above, there are multiple solutions for this issue. Let us look at each of these solutions.

Solution 1: Install all the package.json dependencies

We can do this by opening the terminal in your project’s root directory where the package.json is present and running the below command on our terminal.

# Using NPM
npm install

# Using yarn
yarn

The npm install will ensure that all the dependencies that are present in the package.json will be installed into the node_modules folder.

If the installation fails for some reason, you can try to use the force flag as shown below.

npm install --force

Once the installation is completed, run the project using the npm start command, and it should work perfectly.

Solution 2 – Install the React Scripts

In certain cases, the react-scripts package may not be present in the system, which will lead to this issue.

Install the react-scripts package by using the following command.

# Using NPM
npm install react-scripts

# Using yarn
yarn add react-scripts

Solution 3 – Corrupted package-lock.json

If the above solutions do not work for you, then either the package-lock.json or node_modules might be corrupted. We can fix it by following the steps mentioned below.

  • Delete the node_modules folder using the below command
  • Delete the package-lock.json /yarn.lock file.
  • Clean the npm cache and reinstall the dependencies.
# 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

Solution 4 – Ensure react-scripts are present in the dependencies object

Ensure that the react-scripts should be present in the dependencies object and not in the devDependencies.

{
  "dependencies": {
    "react-scripts": "5.0.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}

The react-scripts should not be installed globally, and they should be a project dependency. Having it installed globally may conflict with the version and may cause several issues.

Conclusion

The react-scripts: command not found error mainly occurs if the dependencies are not installed or if the package-lock.json is corrupted, or if the react-scripts is not installed.

We can resolve the issue by deleting the node_modules folder and package-lock.json file and performing the fresh installation of the dependencies, and running the project by using npm start.

[ad_2]

Source link