stegripe / rawon

A simple powerful Discord music bot built to fulfill your production desires. Easy to use, with no coding required.
https://rawon.stegripe.org
GNU Affero General Public License v3.0
1.46k stars 1.69k forks source link

[Bug Report] ES modules must be explicitly defined (`ERR_UNSUPPORTED_DIR_IMPORT`) #1356

Closed ricky8k closed 8 months ago

ricky8k commented 8 months ago

Describe the bug

Since commit https://github.com/stegripe/rawon/pull/1335/commits/24636b0f25f8b776c3554cc36fd2255485b38536, Rawon uses got version 14.0.0. This dependency requires a version of Node v20.x.x or above:

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'got@14.0.0',
npm WARN EBADENGINE   required: { node: '>=20' },
npm WARN EBADENGINE   current: { node: 'v18.17.1', npm: '9.6.7' }
npm WARN EBADENGINE }

At the moment, I'm unsure whether this will become a major issue for Rawon's core functionality. I currently use Rawon as a private music bot for my servers, and the bot continues to operate as expected under Node v18.17.1 with the unsupported dependency.

However, I did try to install using the latest LTS version of Node (v20.10.0) to see if it would be resolved. Installing got seems to no longer provide an error, but now Rawon refuses to start:

user@mihari:~/rawon$ npm run start

> rawon@3.2.1 start
> node --es-module-specifier-resolution=node -r dotenv/config .

node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/home/user/rawon/yt-dlp-utils' is not supported resolving ES modules imported from /home/user/rawon/index.js
Did you mean to import ../yt-dlp-utils/index.js?
    at finalizeResolution (node:internal/modules/esm/resolve:249:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///home/user/rawon/yt-dlp-utils'
}

Node.js v20.10.0

Based on the fix reported in https://github.com/stegripe/rawon/issues/1338, it seems that you now need to explicitly define ES modules down to the main file in order to import directories properly.

In my limited testing, I've had to change two files to get Rawon running:

There may be more files that need to be changed to fix core functions. I'm unsure whether Rawon utilizes the --experimental-specifier-resolution for Node, which looks like it was removed in v19 and above. As a result of being on v20, I tried to use a custom loader to fix the issue. Unfortunately, my knowledge in Node.js is very limited and I was unable to get it working.

I hope I've managed to provide enough context to the issue!

To Reproduce

  1. Install Node.js v20.10.0 (presumably anything v19.x.x or later as well, though this is unconfirmed)
  2. Install Rawon
  3. Start Rawon using npm start or npm run start

Expected behavior

Rawon should be able to run without issue.

Screenshots

No response

Operating System

Debian 11 (aarch64)

Additional context

No response

Mednoob commented 8 months ago

Yeah, I've been noticed about this by another team member of the project yesterday. Same as you, I'm still unable to use the loader thingy. I can't understand a thing from the docs.

For now, you can use the fix you detailed in your issue as a workaround while we try to fix this.

Mednoob commented 8 months ago

This should be fixed with the latest commit I did.

ricky8k commented 8 months ago

This should be fixed with the latest commit I did.

I appreciate your time on fixing this issue. Unfortunately, I'm still unable to run Rawon on the latest commit. I ran a new install of Rawon with Node v20.10.0 and v20.11.0 (latest LTS) and was unable to start it:

user@mihari:~/rawon$ npm start

> rawon@3.2.1 start
> node --es-module-specifier-resolution=node -r dotenv/config .

[INFO] Couldn't find FFmpeg/avconv, trying to install ffmpeg-static...
[INFO] ffmpeg-static has been installed.
[INFO] Starting the bot...
[2024-01-13 20:51:54 (-05)] [info]: [ShardManager] Shard #0 has spawned.
[2024-01-13 20:51:54 (-05)] [info]: [ShardManager] All shards are spawned successfully.
[2024-01-13 20:51:58 (-05)] [info]: Loading 10 events...
[2024-01-13 20:51:59 (-05)] [info]: Events on listener channelUpdate has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener debug has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener error has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener guildBanAdd has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener guildBanRemove has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener interactionCreate has been added.
[2024-01-13 20:51:59 (-05)] [info]: Events on listener messageCreate has been added.
[2024-01-13 20:51:59 (-05)] [error]: EVENTS_LOADER_ERR: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './locale/index.js' is not defined by "exports" in /home/user/rawon/node_modules/date-fns/package.json imported from /home/user/rawon/dist/utils/functions/formatMS.js
[2024-01-13 20:51:59 (-05)] [info]: Done loading events.

I did manage to fix the event load error it by reverting the const declaration change for locales in src/formatMS.js from the recent commits (https://github.com/ricky8k/rawon/commit/b7bcf95bde8502451501de7741e4616831dacbc0). Unfortunately, my fix isn't completely stable. Sometimes, Rawon will stall after loading events:

user@mihari:~/rawon$ npm start

> rawon@3.2.1 start
> node --es-module-specifier-resolution=node -r dotenv/config .

[INFO] Starting the bot...
[2024-01-13 21:05:29 (-05)] [info]: [ShardManager] Shard #0 has spawned.
[2024-01-13 21:05:29 (-05)] [info]: [ShardManager] All shards are spawned successfully.
[2024-01-13 21:05:33 (-05)] [info]: Loading 10 events...
[2024-01-13 21:05:33 (-05)] [info]: Events on listener channelUpdate has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener debug has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener error has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener guildBanAdd has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener guildBanRemove has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener interactionCreate has been added.
[2024-01-13 21:05:33 (-05)] [info]: Events on listener messageCreate has been added.
[2024-01-13 21:05:35 (-05)] [info]: Events on listener ready has been added.
[2024-01-13 21:05:35 (-05)] [info]: Events on listener voiceStateUpdate has been added.
[2024-01-13 21:05:35 (-05)] [info]: Events on listener warn has been added.
[2024-01-13 21:05:35 (-05)] [info]: Done loading events.

When it wasn't stalled, it loaded the necessary commands and Rawon would work without issue. From what I could observe, it boots properly about 5 minutes after Rawon is stopped.

Mednoob commented 8 months ago

Thanks for the report, I just fixed it. Try it again.

ricky8k commented 8 months ago

Rawon works without a problem now. Thank you!