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.

SSR transform doesn't respect hoisting

See original GitHub issue

Describe the bug

In Javascript, imports are hoisted. So the following code works

decamelize("HelloThere")
import decamelize from 'decamelized'

But when transformed in vite ssr, it produces the following

__vite_ssr_import_0__.default("HelloThere");
const __vite_ssr_import_0__ = await __vite_ssr_import__("decamelize");

Which obviously doesn’t work and produces the following error

Cannot access '__vite_ssr_import_0__' before initialization

Originally opened in https://github.com/vitest-dev/vitest/issues/2138 but it’s a Vite issue actually.

I believe this is a bug for two reasons:

  • Valid JavaScript code doesn’t work when using Vite
  • inconsistency between dev and production modes of Vite. (this works when doing a production build, imports are moved to the top of the file)

Reproduction

https://stackblitz.com/edit/vitejs-vite-xhfybw?file=vite.config.js,server.js,entry.js&terminal=dev

Steps to reproduce

Run node server.js and you will see the error.

It’s worth noting that the production SSR build actually works. So this is an inconstintency as well

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 88.67 MB / 32.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.15.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 106.0.5249.103
    Chrome Canary: 108.0.5355.0
    Safari: 15.5
    Safari Technology Preview: 16.4
  npmPackages:
    vite: ^3.1.0 => 3.1.0

Used Package Manager

npm

Logs

Error: Cannot access ‘vite_ssr_import_0’ before initialization at async ESMLoader.import (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:1209286) at async i.loadESM (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:246623) at async handleMainPromise (https://vitejs-vite-xhfybw.w.staticblitz.com/blitz.ea2f6b48b77806aba8e6fe3b6311530cc7674f64.js:6:989295)

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
elliothimmelfarbcommented, Nov 2, 2022

This issue is blocking my team’s ability to use Vitest with Chakra-ui in our monorepo. Are there any updates?

1reaction
bluwycommented, Oct 19, 2022

Yeah I think we could try that. I don’t see any caveats at a glance. Having the import order preserved would be good too for easier debugging.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices to increase the speed for Next.js apps
Fortunately, developers can follow a number of best practices to improve the speed of their Next.js applications. Use server-side rendering.
Read more >
@vue/compiler-ssr | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
vitest cannot access before initialization
The thing is: jest.mock is hoisted. To be able to use a variable before it you need to use var , because it...
Read more >
Simple Linear Regression Using Statgraphics
Note: Statgraphics refers to SSR as the Model Sum of Squares because it ... SSE is differentiate it with respect to and and...
Read more >
Are variables declared with let or const hoisted?
No, they work the same as far as hoisting is regarded. ... The variable 'y' gives a referenceError, that doesn't mean it's not...
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