Skip to content

Build throws error for an imported module that works fine in dev #5607

Closed as not planned

Description

@tarngerine

Describe the bug

For a given package, there are sometimes differences in how it behaves in build/production than from dev. This is one such example, where a module that I import (default export) can be called fine in dev, but it errors in build step.

Reproduction

https://github.com/tarngerine/not-a-function

  • npm install, npm run dev
  • check console, it should log something like below

image

  • now run npm run build

  • expected: no error

  • actual: error (see log)

Logs

TypeError: CodeBlock.extend is not a function
    at file:///Users/tarng/Code/not-a-function/.svelte-kit/output/server/entries/pages/index.svelte.js:3:11
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async Promise.all (index 1)
    at async respond$1 (file:///Users/tarng/Code/not-a-function/.svelte-kit/output/server/index.js:1862:13)
    at async resolve (file:///Users/tarng/Code/not-a-function/.svelte-kit/output/server/index.js:2225:114)
    at async respond (file:///Users/tarng/Code/not-a-function/.svelte-kit/output/server/index.js:2180:22)
    at async visit (file:///Users/tarng/Code/not-a-function/node_modules/@sveltejs/kit/dist/vite.js:1229:20)
[vite-plugin-svelte-kit] 500 /
error during build:
Error: 500 /
    at file:///Users/tarng/Code/not-a-function/node_modules/@sveltejs/kit/dist/vite.js:1128:11
    at save (file:///Users/tarng/Code/not-a-function/node_modules/@sveltejs/kit/dist/vite.js:1347:4)
    at visit (file:///Users/tarng/Code/not-a-function/node_modules/@sveltejs/kit/dist/vite.js:1238:3)

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Pro
    Memory: 219.05 MB / 32.00 GB
    Shell: 3.2.2 - /usr/local/bin/fish
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.5.0 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 102.1.39.111
    Firefox: 94.0.2
    Safari: 15.5
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.62 
    @sveltejs/kit: next => 1.0.0-next.379 
    svelte: ^3.44.0 => 3.49.0 
    vite: ^3.0.0 => 3.0.2

Severity

blocking an upgrade

Additional Information

i implemented this example after the switch to vite 3, so not sure if it would break before that. it's really hard to make a "old svelte project" given how many configs have changed, so i can't easily compare

Activity

  1. Wobbley commented on Jul 19, 2022

    @Wobbley

    I am not 100% sure this is the same issue I had back in March, but I had an issue with a library where it would run in dev mode, but not in build. I got the following answer on Discord:

    I believe it would apply if they have a CJS and ESM version with default export. The issue is that you need to write the code differently in CJS and ESM as they demonstrate on their README: https://github.com/polygon-software/clockify-ts#introduction. But Vite tries to run CJS on the server and ESM in the client and so any code you write ends up broken in one of the two. If you used named exports it works because the syntax is the same in both scenarios

  2. bluwy commented on Jul 19, 2022

    @bluwy
    Member

    This is an inconsistency issue in Vite, but it's also because the @tiptap/extension-code-block package isn't nodejs friendly, hence it's failing in build, but works in dev (because dev runs as part of Vite's bundler, not nodejs). If you inspect both CJS code, which is the one loaded in build and nodejs, you can see that it doesn't export as exports = CodeBlock, but instead exports["default"] = CodeBlock. A fix (like the above comment suggested) is to use named imports, like import { CodeBlock } from '@tiptap/extension-code-block'; as that is exported correctly and works in nodejs.

    Since the package had already export ESM code, which should be used in build, and can also avoid this issue in the first place. You can request for them to use the package exports field to resolve this. See https://publint.bjornlu.com/@tiptap/extension-code-block@2.0.0-beta.42

    Closing as this is out of scope of SvelteKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions