Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:79:19)
at Object.createHash (node:crypto:139:10)
at module.exports (D:\Medical app\facial-emotion-demo\node_modules\webpack\lib\util\createHash.js:90:53)
at NormalModule._initBuildHash (D:\Medical app\facial-emotion-demo\node_modules\webpack\lib\NormalModule.js:401:16)
at D:\Medical app\facial-emotion-demo\node_modules\webpack\lib\NormalModule.js:433:10
at D:\Medical app\facial-emotion-demo\node_modules\webpack\lib\NormalModule.js:308:13
at D:\Medical app\facial-emotion-demo\node_modules\loader-runner\lib\LoaderRunner.js:367:11
at D:\Medical app\facial-emotion-demo\node_modules\loader-runner\lib\LoaderRunner.js:233:18
at context.callback (D:\Medical app\facial-emotion-demo\node_modules\loader-runner\lib\LoaderRunner.js:111:13)
at D:\Medical app\facial-emotion-demo\node_modules\babel-loader\lib\index.js:51:103 {
opensslErrorStack: [
'error:03000086:digital envelope routines::initialization error',
'error:0308010C:digital envelope routines::unsupported'
],
library: 'digital envelope routines',
reason: 'unsupported',
code: 'ERR_OSSL_EVP_UNSUPPORTED'
}
Node.js v20.17.0
Help me run a react app properly, getting error when doing npm start.
Assistant
The error you're encountering is related to the cryptographic changes in Node.js, particularly with versions 17 and above, which use OpenSSL 3.0. This update introduced stricter security protocols that can cause compatibility issues with some packages, including certain versions of Webpack used in React projects.
Here's a comprehensive guide to resolve the issue:
1. Use the OpenSSL Legacy Provider
The quickest way to bypass this issue is by enabling the legacy OpenSSL provider. This allows Node.js to use older cryptographic algorithms that some dependencies require.
a. Temporary Solution (For Current Session):
Windows Command Prompt:
cmd
set NODE_OPTIONS=--openssl-legacy-provider
npm start
In the root directory of your project, create a .env file if it doesn't exist. Add the following line:
code
NODE_OPTIONS=--openssl-legacy-provider
Ensure React Scripts Load .env:
React scripts automatically load environment variables from the .env file. If you've set up custom configurations, ensure that environment variables are correctly loaded.
2. Downgrade Node.js to a Compatible Version
If the first solution doesn't work or you'd prefer not to use the legacy provider, downgrading Node.js to a version that's compatible with your project's dependencies is a viable option.
a. Using Node Version Manager (nvm):
Install nvm:
If you don't have nvm installed, download and install it from the nvm repository for Unix systems or nvm-windows for Windows.
Install Node.js 16 (LTS Version):
bash
nvm install 16
nvm use 16
Verify the Version:
bash
node -v
# Expected output: v16.x.x
b. Manual Installation:
Download Node.js 16:
Visit the official Node.js downloads page and download the LTS (Long Term Support) version, which is typically version 16.x.x.
Install Node.js:
Follow the installation prompts specific to your operating system.
Verify Installation:
bash
node -v
# Should display v16.x.x
c. After Downgrading:
Clear node_modules and Reinstall:
bash
rm -rf node_modules
npm install
npm start
3. Update Project Dependencies
Sometimes, the issue arises from outdated dependencies that are incompatible with newer Node.js versions. Updating them can resolve the problem.
a. Using npm-check-updates:
Install npm-check-updates:
bash
npm install -g npm-check-updates
Check for Updates:
bash
ncu
Update package.json:
bash
ncu -u
Reinstall Dependencies:
bash
npm install
npm start
b. Manually Update Dependencies:
Open package.json and identify outdated packages, especially react-scripts and webpack.
Update their versions to the latest compatible versions.
Run:
bash
npm install
npm start
⚠️ Note: Updating dependencies might introduce breaking changes. Ensure you test your application thoroughly after updates.
4. Modify Webpack Configuration (Advanced)
If you're comfortable tweaking build configurations, you can adjust the hashing algorithm used by Webpack.
Locate webpack.config.js:
This file might be in the root directory or within a config folder, depending on your project's setup.
Modify the Hash Function:
javascript
const crypto = require('crypto');
// Override the createHash function to use a supported algorithm
crypto.createHash = (algorithm) => crypto.createHash('sha256');
Save and Restart:
bash
npm start
⚠️ Caution: Modifying internal Webpack configurations can lead to unexpected behavior. Ensure you understand the changes or consult the documentation.
Final Recommendations
Start with the Legacy Provider: It's the quickest fix and often resolves the issue without significant changes.
Consider Long-Term Solutions: Downgrading Node.js or updating dependencies ensures better compatibility and security in the long run.
Stay Updated: Regularly update your project's dependencies to benefit from security patches and new features.
If after trying the above solutions the problem persists, please provide additional details about your project setup, including your package.json dependencies, so further assistance can be provided.