As Node 12 LTS is reaching End of Life, in this article we will discuss the most common issues you might have after the upgrade and the options you have to resolve them.
As you know, in Node apps, sometimes there may be Promises which might be rejected, but the rejections are never caught.
Previously if that happened we saw the following warning message on the console:
In Node JS 15 and above versions the default behavior has been changed. Now instead of getting a warning message, the process will crash with the following error:
If you are still on NodeJS 12 LTS or 14 LTS versions, it’s recommended that you upgrade to the latest LTS version (currently v16). If you see UnhandledPromiseRejectionWarning messages in your logs, then there is a chance that your Node app will crash after the upgrade. We suggest 3 ways to handle these types of issues in the newest version of NodeJS:
- Find and properly handle all the unhandled promise rejections in the code:
Achieving this in real world applications might be time consuming. In our opinion, this option should be gradually implemented over the long run.
- Add global listener for unhandled promise rejections:
Despite the fact that this option will resolve the process termination issue, the recommendation is to use this as a midterm solution. Logging all the unhandled promises will provide a full picture. This information can then be used later on to properly handle all the remaining unhandled rejections.
- Change the behavior to act similar to pre-15 versions, when starting the node process:
a. node --unhandled-rejections=warn index.js
b. NODE_OPTIONS=--unhandled-rejections=warn node index.js
This option is the fastest to use as it changes only the command we are using for starting the node app.
At Insightful, we use Node JS for our backend. We have microservices architecture powered by Moleculer Framework and Kubernetes acts as a container orchestrator.
We also recently decided to upgrade the NodeJS version. We were still on v12 and its support was ending in April 2022. So we decided to go with the 3rd option: adding --unhandled-rejections=warn flag as an interim solution, until we completely resolve the unhandled rejections.
This approach required changes to the Dockerfile and the Kubernetes Deployment files, as follows:
Deployment yaml file
After building the new image and deploying to Kubernetes via the config mentioned above, no application crashes have been observed.
To summarize: if you want to painlessly upgrade Node JS from 12 to 16, you can select one of the options mentioned in this article. Please choose your option wisely based on your use case.