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:
- "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
- Upgraded nodejs to v21.4.0 using nvm. However, the bot's error still persists.
- 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. - 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.
- 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
. - According to this search result, upgraded pm2 and restarted the pm2 process:
Then checked again using
pm2 update pm2 restart app --update-env
pm2 show
, and this time the nodejs version was successfully upgraded. Tested the bot again and it worked.