question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic imports fail to load in dev SSR

See original GitHub issue

Describe the bug When importing modules dynamically in a page’s load function, it only works when the import is a static string and using single/double quotes (not backticks). However, they work in production. It works client-side in dev as well.

Logs No logs other than the error itself (check the stack trace)

To Reproduce

Add this to any route component or download this repro https://github.com/mattjennings/sveltekit-dynamic-import-bug

<script context="module">
  export async function load({ page }) {
    await import("../lib/Template.svelte"); // works
    await import(`../lib/Template.svelte`); // does not work (note the backticks)
    await import(`../lib/${page.params.slug}.svelte`); // does not work
    await import("../lib/" + "Template" + ".svelte"); // does not work

    return {
      props: {},
    };
  }
</script>

Expected behavior All imports in snippet should work in dev

Stacktraces

The same error happens for every bad import. Unfortunately, it’s not a very helpful error. It would be nice to include a bit more info.

Stack trace
failed to load module for ssr: ../lib/Template.svelte
Error: failed to load module for ssr: ../lib/Template.svelte
    at instantiateModule (/Users/mattjennings/Desktop/dynamic/node_modules/.pnpm/vite@2.2.4/node_modules/vite/dist/node/chunks/dep-2c03f3f9.js:69022:15)

Information about your SvelteKit Installation:

Diagnostics
  • System: OS: macOS 11.2.3 CPU: (16) x64 Intel® Core™ i9-9880H CPU @ 2.30GHz Memory: 97.98 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 15.11.0 - ~/.nvm/versions/node/v15.11.0/bin/node Yarn: 1.22.10 - ~/.nvm/versions/node/v15.11.0/bin/yarn npm: 7.6.0 - ~/.nvm/versions/node/v15.11.0/bin/npm Browsers: Brave Browser: 90.1.23.75 Chrome: 90.0.4430.93 Safari: 14.0.3 Safari Technology Preview: 14.2 npmPackages: @sveltejs/kit: next => 1.0.0-next.96 svelte: ^3.34.0 => 3.38.2 vite: ^2.2.3 => 2.2.4

  • Chrome

  • Tried with no adapter & vercel adapter

Severity It’s annoying in dev but navigating to the page client-side is a fine workaround for now, and it works in production so it’s not critical.

Additional context My specific use-case here was to load a .md file dynamically from a param in the route.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
benblazakcommented, Nov 26, 2021

For anyone running across this because it’s one of the only google results for Unknown variable dynamic import.

I was getting this error when building (not in dev) because the variable I had in my dynamic import had a / in it. Turns out vite doesn’t like that: https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#globs-only-go-one-level-deep .

1reaction
Eugeniegranditcommented, Jul 8, 2021

Thank you @vish01! I tried to change my path a little and it worked. Actually, the folder I was trying to access is in the same folder as my file, but instead of ./ I used …/…/src/routes/ and surprisingly, the warning disappeared in the console. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

SSR: dynamic import issue - reactjs - Stack Overflow
import() needs to be explicitly written without template strings and dynamic() can't be used inside of React rendering as it needs to be...
Read more >
Understanding React dynamic imports for faster websites
However, as soon as we attempt to host and use the program, it is clear that the load time is excessive, and the...
Read more >
Module Methods - webpack
Dynamically load modules. Calls to import() are treated as split points, meaning the requested module and its children are split out into a...
Read more >
Features | Vite
Vite will detect such bare module imports in all served source files and perform ... Matched files are by default lazy-loaded via dynamic...
Read more >
Using Non-SSR Friendly Components with Next.js
Next.js claims to support ES2020 dynamic import() for JavaScript. It allows you to import JavaScript modules dynamically and works collaboratively with SSR as ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found