This article is about fixing react-scripts fails to build project with Node 17
  • 31-Jan-2023
Lightrun Team
Author Lightrun Team
Share
This article is about fixing react-scripts fails to build project with Node 17

React-scripts fails to build project with Node 17 in Facebook Create React App

Lightrun Team
Lightrun Team
31-Jan-2023

Explanation of the problem

A CRA app failed to build after upgrading Node.js from 14.x to 17.0.0. The following error was encountered: “Error: error:0308010C:digital envelope routines::unsupported.” The problem occurred on both the local machine and the CI/CD machine after attempting to recover all dependencies.

Technical description:

  • CRA app version: 4.0.3
  • Running from: /home/.npm/_npx/34113/lib/node_modules/create-react-app
  • System: Linux 5.13 Ubuntu 21.10 21.10
  • Binaries:
    • Node: 17.0.0 – /usr/bin/node
    • Yarn: 1.22.10 – /usr/bin/yarn
    • npm: 7.17.0 – /usr/bin/npm
  • Error logs:
    • Error: error:0308010C:digital envelope routines::unsupported
    • Reason: unsupported
    • Code: ERR_OSSL_EVP_UNSUPPORTED
  • The error was caused by the “Hash” module from the Node.js internal crypto library.
  • The error occurred in the following modules:
    • /my-project/node_modules/webpack/lib/util/createHash.js
    • /my-project/node_modules/webpack/lib/NormalModule.js
    • /my-project/node_modules/loader-runner/lib/LoaderRunner.js
    • /my-project/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js
    • /my-project/node_modules/react-scripts/scripts/build.js

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for React-scripts fails to build project with Node 17 in Facebook Create React App

The first solution is to include the “–openssl-legacy-provider” flag when building the project with React-scripts. This flag allows the build process to use an older version of OpenSSL, which is compatible with Node 17. To use this solution, modify the build command in the package.json file to include the “–openssl-legacy-provider” flag, like this:

"scripts": {
  "build": "react-scripts build --openssl-legacy-provider"
}

The second solution is to use the “if-node-version” package to provide different build commands based on the version of NodeJS being used. This package allows for a more dynamic approach to resolving compatibility issues between NodeJS and OpenSSL. To use this solution, first, install the “if-node-version” package:

npm install if-node-version

Then, modify the build command in the package.json file to use the “if-node-version” package, like this:

"scripts": {
  "build": "if-node-version 17 react-scripts build --openssl-legacy-provider || react-scripts build"
}

This will use the “–openssl-legacy-provider” flag only when building with Node 17, and the default React-scripts build command for other NodeJS versions.

Other popular problems with Facebook Create React App

Problem: Missing Module Error

One of the most common problems with Facebook’s Create React App is the “Module not found” error. This error occurs when a required module is missing from the project’s dependencies.

Solution:

The solution to this problem is to install the missing module using npm or yarn. For example, if the missing module is called “lodash”, you can install it using the following command:

npm install lodash
or
yarn add lodash

It is important to note that you need to re-run the above command every time you encounter a missing module error in your project.

Problem: Build Fails Due to OpenSSL Compatibility Issues

Another common problem with Facebook’s Create React App is compatibility issues with the version of OpenSSL being used. React-scripts, which is used for building React projects, may fail to build the project due to compatibility problems with the version of OpenSSL. There are two possible solutions to this problem.

Solution:

The first solution is to include the “–openssl-legacy-provider” flag when building the project with React-scripts. The second solution is to use the “if-node-version” package to provide different build commands based on the version of NodeJS being used.

Problem: Error: Maximum Call Stack Size Exceeded

The “Maximum call stack size exceeded” error is another common problem in Facebook’s Create React App. This error occurs when a function is called recursively, leading to an infinite loop.

Solution:

To solve this problem, you need to find the source of the recursive function call and modify it to stop the recursion. This can be done by adding a conditional statement to the function that stops the recursion once a certain condition is met. It is important to thoroughly test your solution to ensure that the infinite loop has been eliminated.

A brief introduction to Facebook Create React App

Facebook’s Create React App is a tool for creating and building React projects quickly and efficiently. It provides a command-line interface (CLI) that automates the setup and configuration of a new React project, eliminating the need for manual setup and configuration. Create React App also provides a number of features to make the development process easier, such as a development server with live reloading, a build pipeline with code minification and optimization, and a set of default configurations for popular tools and libraries used in React development.

One of the key benefits of using Create React App is the ability to focus on writing code, without having to worry about the underlying build and configuration process. Create React App abstracts away the complexity of setting up a React project, making it easier for developers to get started with a new project and start coding immediately. Additionally, the CLI provides a consistent development experience for all developers, regardless of the operating system or text editor being used. This consistency ensures that the codebase remains stable and maintainable, even as the team grows and evolves over time.

Most popular use cases for Facebook Create React App

  1. Building and Deploying React Applications:

Facebook’s Create React App can be used to build and deploy React applications. The CLI provides a simple and efficient way to create a new React project, with all the necessary configurations and dependencies automatically set up. The build pipeline of Create React App makes it easy to compile, minify, and optimize the code, ready for deployment to production.

npx create-react-app my-app
cd my-app
npm start
  1. Consistent Development Environment:

Create React App can be used to ensure a consistent development environment for all members of a team. The CLI provides a set of default configurations for popular tools and libraries used in React development, ensuring that all members of the team are using the same version of these tools and libraries. This consistency helps to avoid conflicts and makes it easier to collaborate on a project.

  1. Improving Productivity:

Create React App can be used to improve developer productivity. The CLI eliminates the need for manual setup and configuration of a React project, freeing up time for developers to focus on writing code. Additionally, the development server with live reloading makes it easy to see changes to the code in real-time, without having to manually reload the page. This improves the development workflow and allows developers to work more efficiently.

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By clicking Submit I agree to Lightrun’s Terms of Use.
Processing will be done in accordance to Lightrun’s Privacy Policy.