Skip to content

[vite-node] Doesn't respect import hoisting #2138

@haikyuu

Description

@haikyuu

Describe the bug

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

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

But when transformed in vite-node, 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

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-jw9dyp?file=index.js&initialPath=__vitest__

  • run node index.js
  • run vite-node index.js and you will see the error

System Info

System:
    OS: macOS 12.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.60 GB / 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.5352.2
    Safari: 15.5
    Safari Technology Preview: 16.4
  npmPackages:
    vite: ^3.1.0 => 3.1.0

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions