digital envelope routines::unsupported

Original link: https://blog.othree.net/log/2023/09/15/digital-envelope-routines-unsupported/

Node.js 16 LTS has ended maintenance, so the things on hand start to need to be upgraded, and then I have to face this error message that I have been avoiding for a long time:

 digital envelope routines::unsupported

This error basically occurs in several website projects, and it is especially easy to see during build projects. Moreover, this error actually looks different from the commonly seen JS errors. The whole picture is actually like this:

digital envelope routines::unsupported

 Error: error:0308010C:digital envelope routines::unsupported

The first is the error message. There are some hex values ​​in front of it. I don’t know what they are. Then in the trace below, you can see that almost all of them are things in node_module. They are not caused by our own code, so it is very confusing. I think Tell me if it is a system problem or if there is some incompatibility caused by stealing a non-public API. In short, I encountered this problem before and then downgraded it again without looking into it carefully. This time I finally have to deal with it seriously. However, the search results almost all said to add an --openssl-legacy-provider flag, but no one said what it is. After searching for this question for a long time, I finally found the most correct answer on StackOverflow. I didn’t expect it to be related to the fact that the life of OpenSSL 1.x has come to an end.

It turned out that this error was actually caused by the switch from OpenSSL 1.x to 3.x starting from Node.js 17, and then OpenSSL 3.x was not backward compatible, so some things had a chance to go wrong, and what was broken here was actually Some legacy hash methods are removed by default. When Webpack creates a bundle file, if the file name uses hash, the default hash method uses md4 , which has been eliminated, and md4 uses Node.js. crypto to call OpenSSL to do things. The Node.js document also mentions that the supported algorithms are based on your OpenSSL version and system, so there is no guarantee that md4 can be used. If you use an algorithm that OpenSSL does not support , the error message that came out was as special as the screenshot above. Then I specially ran it with OpenSSL 3 cli, and the error message that came out was really the same:

OpenSSL 3 error

I actually think it’s acceptable to use a flag to turn on support for old algorithms. After all, it’s just a build, not a service. However, this flag seems a bit special. It seems that it cannot be placed directly in NODE_OPTIONS , and if you get the same library When running in the old version of Node.js environment, adding this flag will prevent it from running, so the best thing is to solve the problem.

So how should this problem be solved? In fact, to put it simply, just upgrade the suite, because the new version of the current suite has dealt with this problem, but before embarking on the upgrade path, you can try the solutions on StackOverflow (it may make your project rotten, please Backup first):

 npm audit fix --force

If you are using yarn, there is no audit fix available, but some people also provide a repair process using npm, but I have not tried this process. I have a project of my own that relied on yarn upgrade to solve the problem (actually Upgrade all the useful loader-utils to 2.0.4 (originally there is a package that uses 2.0.0), and the rest that cannot be repaired must be done manually. And because the only websites I deal with are Gatsby and CRE( Create React App), so the following is mainly about these two systems. Both actually use Webpack as the packaging tool, and Webpack is guaranteed to support Node.js 17 starting from v5.61.0. I will I checked that Gatsby starts from 4.2.0 , while CRA requires the latest version of react-script 5.0.1 to guarantee support. Why is it said to be guaranteed? Because of the semver range of ^ , for example, if your react-script is 5.0.0, then your local installation may be Webpack v5.60.0, which does not support Node.js 17. Like me, I have Gatsby 3. If I upgrade to 4.x, it will be fine.

Gatsby and CRA are actually fine. The worst thing is CRA that has been ejected. You can only upgrade it manually. Basically, you just go to react-script and copy the required files back to your project. The most important thing is files under scripts/ and config/ , and then change back the changes you have made based on your own modification records, and then update the dependencies in package.json . The version number is based on the package.json in react-script. The most important thing is It’s related to webpack . Then install the package and rebuild it. If you still get the same error, look at the trace to see which dependent package it is. If there is a new version with corrections, try updating it. That’s probably it. It’s easy to miss things, so it’s It takes a lot of time to keep repeating the test, but in the end it is a sense of accomplishment when the build is successful.

PS. You should also be careful about the sequelae of other upgrades. If it is an app, it is best to test various behaviors. For example, I encountered the problem that Webpack 5 does not support polyfill Buffer. That error happened to be caught again, so my build was fine. I just couldn’t run the test, so I dealt with it later by referring to articles on the Internet.

This article is reproduced from: https://blog.othree.net/log/2023/09/15/digital-envelope-routines-unsupported/
This site is only for collection, and the copyright belongs to the original author.