Saltad

Saltad

【Bug Log】Error caused by outdated version of Node.js in pm2 process.

Date: 2023.12.17

Issue: When developing a discord bot, encountered the error "Error: Cannot find module 'readline/promises'" on the server side. However, there were no errors locally.

Solution Process:

  1. "readline/promises" is a library used by the bot's dependency, elephant-sdk. According to this search result, it is initially believed that it may be related to the version of nodejs.

Checked the server's nodejs version, which is v16.19.1, suspecting that the error is caused by a low version of nodejs on the server.

root@nunti-bot:~# node -v
v16.19.1

  1. Upgraded nodejs to v21.4.0 using nvm. However, the bot's error still persists.
  2. Attempted to minimize the error. Ran the code in the Node.js REPL (Read-Eval-Print Loop):
    const readline = require('readline/promises');
    No error occurred.
  3. Downloaded the elephant sdk separately, installed dependencies using npm (the same package manager as the bot), and ran the example.ts provided in the repository, which ran successfully.
  4. At this point, there was no clear direction. After putting it aside for a while, I suddenly realized before going to bed that the bot had been restarted during testing using pm2. It is likely that pm2 is still using the old version of nodejs. To verify this hypothesis, I used pm2 show 'app-name' to check and indeed found a line in the returned result: │ node.js version │ 16.19.1.
  5. According to this search result, upgraded pm2 and restarted the pm2 process:
    pm2 update
    pm2 restart app --update-env
    
    Then checked again using pm2 show, and this time the nodejs version was successfully upgraded. Tested the bot again and it worked.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.