sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.45k stars 1.89k forks source link

Module imported as CommonJS, even when ES6 module exists #3246

Closed kraxarn closed 2 years ago

kraxarn commented 2 years ago

Describe the bug

I'm trying to import the "@heroiclabs/nakama-js" library into a SvelteKit project, but building always fails as it thinks the library is a CommonJS module. The "package.json" file in the library has a CommonJS file specified in "main", but also a ES6 module specified in "module", which seems to be ignored. I can't seem to reproduce the issue with Firebase for example, which has a similar structure, but doing the same steps from a vite project seems to work fine, so I don't think it's a bug in the library itself. A workaround I've found is to manually edit the "package.json" file of the library and changing the "main" to use the module file, instead of the CommonJS file.

Reproduction

StackBlitz

Steps:

Logs

❯ yarn run build
$ svelte-kit build
vite v2.7.10 building for production...
✓ 15 modules transformed.
.svelte-kit/output/client/_app/manifest.json                    1.21 KiB
.svelte-kit/output/client/_app/start-311010a3.js                19.58 KiB / gzip: 6.78 KiB
.svelte-kit/output/client/_app/layout.svelte-8f8cfb61.js        0.53 KiB / gzip: 0.35 KiB
.svelte-kit/output/client/_app/error.svelte-20cdcf24.js         1.56 KiB / gzip: 0.75 KiB
.svelte-kit/output/client/_app/pages/index.svelte-cf1af39f.js   0.83 KiB / gzip: 0.48 KiB
.svelte-kit/output/client/_app/chunks/vendor-2e3f37b6.js        15.73 KiB / gzip: 5.85 KiB
.svelte-kit/output/client/_app/assets/start-61d1577b.css        0.16 KiB / gzip: 0.15 KiB
vite v2.7.10 building SSR bundle for production...
✓ 12 modules transformed.
.svelte-kit/output/server/manifest.json                    0.92 KiB
.svelte-kit/output/server/app.js                           46.82 KiB
.svelte-kit/output/server/entries/pages/layout.svelte.js   0.24 KiB
.svelte-kit/output/server/entries/pages/error.svelte.js    0.72 KiB
.svelte-kit/output/server/entries/pages/index.svelte.js    0.38 KiB
.svelte-kit/output/server/chunks/index-2dc61825.js         2.24 KiB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-static
file:///home/projects/sveltekit-tqnuuh/.svelte-kit/output/server/entries/pages/index.svelte.js:2
import { Client } from "@heroiclabs/nakama-js";
         ^^^^^^
SyntaxError: Named export 'Client' not found. The requested module '@heroiclabs/nakama-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 '@heroiclabs/nakama-js';
const { Client } = pkg;

    at async respond$1 (file:///home/projects/sveltekit-tqnuuh/.svelte-kit/output/server/app.js:952:13)
    at async render_page (file:///home/projects/sveltekit-tqnuuh/.svelte-kit/output/server/app.js:1111:20)
    at async resolve (file:///home/projects/sveltekit-tqnuuh/.svelte-kit/output/server/app.js:1311:104)
    at async respond (file:///home/projects/sveltekit-tqnuuh/.svelte-kit/output/server/app.js:1292:12)
> 500 /
    at eval (file:///home/projects/sveltekit-tqnuuh/node_modules/@sveltejs/kit/dist/chunks/index5.js:178:11)
    at visit (file:///home/projects/sveltekit-tqnuuh/node_modules/@sveltejs/kit/dist/chunks/index5.js:337:5)

System Info

System:
    OS: Linux 5.15 Arch Linux
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 9.03 GB / 31.27 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 17.3.0 - /usr/bin/node
    Yarn: 1.22.17 - /usr/bin/yarn
    npm: 8.3.0 - /usr/bin/npm
  Browsers:
    Chromium: 97.0.4692.71
    Firefox: 95.0.2

Severity

annoyance

Additional Information

No response

bluwy commented 2 years ago

Note: Crosspost https://github.com/heroiclabs/nakama-js/issues/107

bluwy commented 2 years ago

I made a pull request upstream to fix this: https://github.com/heroiclabs/nakama-js/pull/108. Closing in favor of it.