Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Named export 'CancelablePromise' not found #1058

Closed
raimund-schluessler opened this issue Aug 18, 2024 · 4 comments
Closed

bug: Named export 'CancelablePromise' not found #1058

raimund-schluessler opened this issue Aug 18, 2024 · 4 comments

Comments

@raimund-schluessler
Copy link

I get this error

SyntaxError: Named export 'CancelablePromise' not found. The requested module 'cancelable-promise' 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 'cancelable-promise';
const { CancelablePromise } = pkg;

when trying to run npm run test with @nextcloud/files@3.8.0, see here raimund-schluessler/inventory#610. It seems there is a problem with the import here

import { CancelablePromise } from 'cancelable-promise'

Is this an error with the package, or do I use it wrong? It used to work fine with 3.1.1.

@skjnldsv
Copy link
Contributor

cc @susnux

@susnux
Copy link
Contributor

susnux commented Aug 18, 2024

The import is correct, it is not a CJS module but provides both.
Also our ESM entry correctly imports it and the CJS entry also correctly requires the CJS entry.

So the problem here is that the cancelable-promise package does not correctly provide their exports,
they provide "main" and "module" but this is ignored and the package does not provide "export".
Vitest will pass all dependencies (node_modules) directly to Node without the Vite pipeline, but this will not work here (you can see that build works because of Vite but tests do not).

Fixes possible:

  1. Upstream add the missing exports
  2. You inline the dependency so it gets transpiled by Vite

In general all @nextcloud libraries do not target Node but the Browser, so I think it would be the safest to change your config to:

     server: {
       deps: {
-        inline: ['@nextcloud/vue'],
-      }
+        inline: [/@nextcloud\//],
+      },
     },

@raimund-schluessler
Copy link
Author

@susnux Thanks a lot for looking into this! Inlining the dependencies works. Is it a general recommendation to inline all nextcloud dependencies? We might stumble upon this more often, when we migrate more apps to vite/st I suppose.

Closing this issue, since there is a fix.

@susnux
Copy link
Contributor

susnux commented Aug 19, 2024

Well we have the same problem with other test runners like jest too.
The problem is they are meant for Node, so they think packages are for Node.js, but we do not target Node.js but Browsers, so our packages are built with Browsers in mind.

Meaning most of the test runners try to boost the performance by just letting Node execute the dependencies instead of transpiling. It is not really related to vite in this case.

But to answer your question: Yes I would recommend to always transpile @nextcloud/ libraries for the reasons mentioned :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants