jellyfin-discord-music-bot icon indicating copy to clipboard operation
jellyfin-discord-music-bot copied to clipboard

Error [ERR_REQUIRE_ESM]: require() of ES Module - Jellyfin SDK 0.8.1 ESM

Open manuel-rw opened this issue 1 year ago • 0 comments

Describe the bug In the commit https://github.com/jellyfin/jellyfin-sdk-typescript/pull/341/files, the SDK was made to target ES6 properly. During an automated pull request, that updated from the SDK 0.7.0 to 0.8.1 (https://github.com/manuel-rw/jellyfin-discord-music-bot/pull/154), the project broke. According to https://stackoverflow.com/questions/74830166/unable-to-import-esm-module-in-nestjs/75287028#75287028 and https://github.com/nestjs/nest/pull/8736, Nestjs does not support ES modules. This causes any following Github Actions to fail and not build the image.

I temporarily reverted this in https://github.com/manuel-rw/jellyfin-discord-music-bot/pull/166 to make the CI pass again.

To Reproduce Steps to reproduce the behavior:

  1. Upgrade @jellyfin/sdk to 0.8.1 or higher
  2. Run yarn start

Expected behavior The build and tests are passing.

Actual behaviour:

yarn run v1.22.19
$ nest start
 
E:\jellyfin-discord-music-bot\dist\clients\jellyfin\jellyfin.search.service.js:14
const models_1 = require("@jellyfin/sdk/lib/generated-client/models");
                 ^
Error [ERR_REQUIRE_ESM]: require() of ES Module E:\jellyfin-discord-music-bot\node_modules\@jellyfin\sdk\lib\generated-client\models\index.js from E:\jellyfin-discord-music-bot\dist\clients\jellyfin\jellyfin.search.service.js not supported.
Instead change the require of index.js in E:\jellyfin-discord-music-bot\dist\clients\jellyfin\jellyfin.search.service.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (E:\jellyfin-discord-music-bot\dist\clients\jellyfin\jellyfin.search.service.js:14:18)
    at Object.<anonymous> (E:\jellyfin-discord-music-bot\dist\clients\jellyfin\jellyfin.module.js:14:35)
    at Object.<anonymous> (E:\jellyfin-discord-music-bot\dist\clients\discord\discord.module.js:14:27)
    at Object.<anonymous> (E:\jellyfin-discord-music-bot\dist\app.module.js:19:26)
    at Object.<anonymous> (E:\jellyfin-discord-music-bot\dist\main.js:4:22)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Additional context It might be possible to work around this with dynamic modules (Stackoverflow issue for context). The Jellyfin SDK could also be made for commonjs, but that might be a lot of additional work. I think it's best to keep up with updates and use a workaround, until Nestjs implemented proper handling for ESM.

The ideal solution would be, to use ESM instead. However, the PR in Nestjs has been stale for over 2 years. I doubt that there will be any progress on it anytime soon. If there should be support for this someday, the asynchronous imports should be reverted.

manuel-rw avatar Apr 11 '23 14:04 manuel-rw