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]: 'type' declaration inside package.json is missing, causing it to be considered a CommonJS module #660

Open
greybaron opened this issue Mar 17, 2024 · 3 comments
Labels
bug:confirmed bug Something isn't working

Comments

@greybaron
Copy link

greybaron commented Mar 17, 2024

Expected Behavior

Importing the module works as it does in StackBlitz SvelteKit example

Current Behavior

After importing the dependency, multiple errors are thrown about cookieconsent being a CommonJS module:

SyntaxError: [vite] Named export 'run' not found. The requested module 'vanilla-cookieconsent' 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 'vanilla-cookieconsent';
const {run} = pkg;

[multiple similar errors...]

the workarounds provided by the error messages are ineffective.

Steps to reproduce

Generate a new SvelteKit 2.x project, using Node 20.x
Recreate the StackBlitz example

Proposed fix or additional info.

Since this is an ES module, the following property should be set inside package.json:
"type": "module",
Without this property, Node considers this to be a CommonJS module and throws multiple errors.

Note: I assume this behaviour was introduced between Node 18.18 and 20.x, as it works on Node 18 on StackBlitz

Version

3.0.0

On which browser do you see the issue?

Other

@greybaron greybaron added the bug Something isn't working label Mar 17, 2024
@github-actions github-actions bot added the triage yet to be reviewed label Mar 17, 2024
@orestbida
Copy link
Owner

I can confirm the issue.

Although the #661 does fix this setup, it also breaks the test suite, as jest (the test framework) doesn't support es modules.

The workaround, for now, is to use the import * syntax:

import * as CookieConsent from 'vanilla-cookieconsent';

@orestbida orestbida added bug:confirmed and removed triage yet to be reviewed labels Mar 21, 2024
@rbozan
Copy link

rbozan commented Jun 3, 2024

I can confirm the issue.

Although the #661 does fix this setup, it also breaks the test suite, as jest (the test framework) doesn't support es modules.

The workaround, for now, is to use the import * syntax:

import * as CookieConsent from 'vanilla-cookieconsent';

OK but could the Sveltekit example be updated to reflect this? Now the SvelteKit users have to figure this out by themselves.

@orestbida
Copy link
Owner

@rbozan sure, updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug:confirmed bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants