Quick fix for error:0308010C:digital envelope routines::unsupported

This tutorial explains about solution for error:0308010C:digital envelope routines::unsupported

Solution for error:0308010C:digital envelope routines::unsupported

Today, When i am starting the nuxtjs node application, got the below error

node:internal/crypto/hash:71
  this[kHandle] = new _Hash(algorithm, xofLen);
                  ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:71:19)
    at Object.createHash (node:crypto:133:10)
    at module.exports (A:\work\nuxtapp\node_modules\webpack\lib\util\createHash.js:135:53)
    at NormalModule._initBuildHash (A:\work\nuxtapp\node_modules\webpack\lib\NormalModule.js:417:16)
    at handleParseError (A:\work\nuxtapp\node_modules\webpack\lib\NormalModule.js:471:10)
    at A:\work\nuxtapp\node_modules\webpack\lib\NormalModule.js:503:5
    at A:\work\nuxtapp\node_modules\webpack\lib\NormalModule.js:358:12
    at A:\work\nuxtapp\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:373:3
    at iterateNormalLoaders (A:\work\nuxtapp\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:214:10)
    at Array.<anonymous> (A:\work\nuxtapp\node_modules\webpack\node_modules\loader-runner\lib\LoaderRunner.js:205:4)
    at Storage.finished (A:\work\nuxtapp\node_modules\webpack\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:55:16)
    at A:\work\nuxtapp\node_modules\webpack\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:91:9
    at A:\work\nuxtapp\node_modules\graceful-fs\graceful-fs.js:123:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
  library: 'digital envelope routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

My Current Node version is Node.js v18.12.0

  • What is the cause for this error?

The error is reproduced with the NuxtJS application with NodeJS v18.x.x. It is an issue with OpenSSL 3.

Before Node 17.x versions, It uses OpenSSL 2 version. NodeJS uses OpenSSL for hash functionality code. The OpenSSL 3 disables MD4, Due to that nodejs is broken in the latest nodejs versions. There is an issue with OpenSSL 3 for modification of code for initialization of MD family which includes the md4 HASH algorithm Function.

You can check more about the issue here🔗

To fix this, We can do it in two ways.

  • Set NODE options for supporting legacy OpenSSL provider

You can add NODE_OPTIONS via the command line or npm scripts

Command line

For Mac and Unix users

export NODE_OPTIONS=--openssl-legacy-provider

For window command line users

set NODE_OPTIONS=--openssl-legacy-provider

For window powershell terminal

$env:NODE_OPTIONS = "--openssl-legacy-provider"

Another way using configuring npm scripts in the package.json file.

add NODE_OPTIONS=--openssl-legacy-provider to npm scripts.

For NuxtJS app:

Change from the below code

{
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate",
    "lint:js": "eslint --ext \".js,.ts,.vue\" --ignore-path .gitignore .",
    "lint": "npm run lint:js",
    "lintfix": "npm run lint:js -- --fix",
    "test": "jest"
  }
}

to

{
  "scripts": {
    "dev": "NODE_OPTIONS=--openssl-legacy-provider nuxt",
    "build": "NODE_OPTIONS=--openssl-legacy-provider nuxt build",
    "start": "NODE_OPTIONS=--openssl-legacy-provider nuxt start",
    "generate": "nuxt generate",
    "lint:js": "eslint --ext \".js,.ts,.vue\" --ignore-path .gitignore .",
    "lint": "npm run lint:js",
    "lintfix": "npm run lint:js -- --fix",
    "test": "jest"
  }
}

For React app: change from below

{
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build"
  }
}

to

{
  "scripts": {
    "start": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start",
    "build": "NODE_OPTIONS=--openssl-legacy-provider react-scripts build"
  }
}

This fix works in all node versions including 18.x.x

Another way is to downgrade the node version from 18 to 16 version

Current Node version

node -v
v18.2.0

downgrade to 16.x version To downgrade, use the lts option for the node nvm command.

nvm install --lts
nvm use --lts

Or you can explicitly mention the version as 16

nvm install 16
nvm use --lts

Another way, if the above fixes are not working

try to audit npm packages

npm audit fix --force

Conclusion

Upgrading the Nodejs version to the latest broke the node applications such as React, NextJS, NuxtJS, gatsby, and Angular apps.

Try to downgrade the node to the LTS version or set node options to enable the OpenSSL legacy provider.