jellyfin / jellyfin-sdk-typescript

TypeScript SDK for Jellyfin
https://typescript-sdk.jellyfin.org
Mozilla Public License 2.0
77 stars 25 forks source link

Named export '__awaiter' not found #487

Closed KeyboardDabbler closed 5 months ago

KeyboardDabbler commented 1 year ago

I have started a project built on nuxt 3.3.1, added the @jellyfin/sdk library to handle user Auth and future api requests. Has anyone had any experience using this stack, is the below a bug or a error on my side? The login end point authenticateUserByName() is working and in response giving me the AccessToken. However there's an issue with specifically, the awaiter function in this library is not working properly with Nuxt. As a result, i have had to manually make changes to the Node.js module files in order to get the library to work.

[Vue Router warn]: uncaught error during route navigation:                                                                               17:02:02
file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/@jellyfin+sdk@0.8.2_axios@1.4.0/node_modules/@jellyfin/sdk/lib/generated-client/common.js:1
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
         ^^^^^^^^^
SyntaxError: Named export '__awaiter' not found. The requested module '../node_modules/tslib/tslib.es6.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '../node_modules/tslib/tslib.es6.js';
const { __awaiter } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async ViteNodeRunner.interopedImport (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:327:28)
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:227:24)
    at async ViteNodeRunner.cachedRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:159:14)
    at async ViteNodeRunner.dependencyRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:207:14)
    at async C:\Users\keyboardDabbler\Desktop\gbstreams\utils\jellyfinSdk.ts:1:31
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:315:5)
[Vue Router warn]: uncaught error during route navigation:                                                                               17:02:02
file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/@jellyfin+sdk@0.8.2_axios@1.4.0/node_modules/@jellyfin/sdk/lib/generated-client/common.js:1
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
         ^^^^^^^^^
SyntaxError: Named export '__awaiter' not found. The requested module '../node_modules/tslib/tslib.es6.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '../node_modules/tslib/tslib.es6.js';
const { __awaiter } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async ViteNodeRunner.interopedImport (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:327:28)
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:227:24)
    at async ViteNodeRunner.cachedRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:159:14)
    at async ViteNodeRunner.dependencyRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:207:14)
    at async C:\Users\keyboardDabbler\Desktop\gbstreams\utils\jellyfinSdk.ts:1:31
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:315:5)
[Vue Router warn]: uncaught error during route navigation:                                                                               17:02:02
file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/@jellyfin+sdk@0.8.2_axios@1.4.0/node_modules/@jellyfin/sdk/lib/generated-client/common.js:1
import { __awaiter } from '../node_modules/tslib/tslib.es6.js';
         ^^^^^^^^^
SyntaxError: Named export '__awaiter' not found. The requested module '../node_modules/tslib/tslib.es6.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '../node_modules/tslib/tslib.es6.js';
const { __awaiter } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async ViteNodeRunner.interopedImport (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:327:28)
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:227:24)
    at async ViteNodeRunner.cachedRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:159:14)
    at async ViteNodeRunner.dependencyRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:207:14)
    at async C:\Users\keyboardDabbler\Desktop\gbstreams\utils\jellyfinSdk.ts:1:31
    at async ViteNodeRunner.directRequest (file:///C:/Users/keyboardDabbler/Desktop/gbstreams/node_modules/.pnpm/vite-node@0.29.7/node_modules/vite-node/dist/client.mjs:315:5)
KeyboardDabbler commented 1 year ago

I found some information in the nuxt documentation link. I have overcome the error for now without manually making changes to the module files.

I will reference the following If you encounter these errors, the issue is almost certainly with the upstream library. They need to fix their library to support being imported by Node.

manuel-rw commented 1 year ago

Just for the record, #341 caused this error. I have it in all of my projects... Apparently, neither Nestjs nor Nextjs support es modules properly. Because there weren't too many Jellyfin API changes, I have been procrastinating finding a solution for it, but you'll probably have to experiment with TS targets and es modules. If I find anything, I'll post it here.

Conversation via Element: image

manuel-rw commented 1 year ago

@Meierschlumpf helped me with https://github.com/ajnart/homarr/pull/1016 . Now working again for our project.

conner-replogle commented 11 months ago

I'm having this issue with SvelteKit is there any fix for it?

szethh commented 10 months ago

same here with sveltekit :/

thornbill commented 10 months ago

As I commented previously this library only ships as an ES module. If you are working in an ecosystem that does not support ES modules, you may need to have a build step to convert the library to a compatible module format.

I am curious if distributing *.mjs files would help in some of these cases, but someone would have to test that.

isaac-mcfadyen commented 9 months ago

If you are working in an ecosystem that does not support ES modules, you may need to have a build step to convert the library to a compatible module format.

According to the SvelteKit docs, it seems they do support ESM but only when "type": "module" is indicated in the package.json, which it doesn't seem to be for tslib (the import that's failing): https://kit.svelte.dev/docs/faq#how-do-i-fix-the-error-i-m-getting-trying-to-include-a-package https://github.com/microsoft/tslib/blob/main/package.json

It doesn't seem tslib wants to change this either for backwards compatibility reasons: https://github.com/microsoft/tslib/issues/180

In dev mode, Vite actually suggests importing using the following. I wonder if this would work as a workaround, but I'm not exactly sure how to change it given that the import for tslib seems to be autogenerated and not actually written this way in the source files.

import pkg from '../node_modules/tslib/tslib.es6.js';
const { __awaiter } = pkg;
jordanf97 commented 7 months ago

Any updates on this? I've not found a way around it.

From my perspective the Jellyfin SDK is being published as an ES module with a transient dependancy which is a CJS module. (at least this is how Vite sees it).

tslib seems to have no intention of bundling their library as an ES module for compatibility reasons which leaves this SDK in an unusable state for myself and others.

I'm just going to either not use the SDK or copy the source files across directly but obviously neither of these solutions are ideal

jordanf97 commented 7 months ago

For what it's worth I found a middle ground solution which is good-enough for me it's probably dumb but hopefully someone else might find it useful too.

I created a package patch where I defined the tslib package as module by creating a package.json file within @jellyfin/sdk/lib/node_modules/tslib/.

The contents of the patch file:

new file mode 100644
index 0000000..1632c2c
--- /dev/null
+++ b/node_modules/@jellyfin/sdk/lib/node_modules/tslib/package.json
@@ -0,0 +1 @@
+{"type": "module"}
\ No newline at end of file
thornbill commented 5 months ago

This should be fixed in the next release, but if you would like to confirm before then, you can test the latest unstable tag.

Update: I've confirmed the issue is fixed for jest in jellyfin-expo at least.