Skip to content

Dynamic imports with a load plugin fail #8

@Finesse

Description

@Finesse

Describe the bug

vite-node fails to handle dynamic imports of plugin-transformed modules, throwing:

Error: The server is being restarted or closed. Request is outdated

The requirements for the bug are:

  1. Having a plugin in the Vite config that defines a custom load function for a file extension.
  2. Importing a file with this extension dynamically in the runtime code.

A workaround for this bug is running vite-node with the --watch flag, and executing process.exit() in the runtime code to finish the script. You can see it by running npm run workaround in the reproducible demo.

Reproduction

  1. Open https://stackblitz.com/edit/vite-node-dynamic-import-bug (backup)
  2. Wait until npm install completes
  3. Execute npm run bug

The output is:

❯ npm run bug

> [email protected] bug
> vite-node index.ts

Script started
Performing dynamic import...
❌ FAILED: Error: The server is being restarted or closed. Request is outdated
    at throwClosedServerError (file:///home/projects/vite-node-dynamic-import-bug/node_modules/vite/dist/node/chunks/config.js:29277:32)
    at EnvironmentPluginContainer.resolveId (file:///home/projects/vite-node-dynamic-import-bug/node_modules/vite/dist/node/chunks/config.js:29409:65)
    at EnvironmentModuleGraph._resolveUrl (file:///home/projects/vite-node-dynamic-import-bug/node_modules/vite/dist/node/chunks/config.js:35382:39)
    at EnvironmentModuleGraph.getModuleByUrl (file:///home/projects/vite-node-dynamic-import-bug/node_modules/vite/dist/node/chunks/config.js:35190:19)
    at doTransform (file:///home/projects/vite-node-dynamic-import-bug/node_modules/vite/dist/node/chunks/config.js:23300:17)

System Info

System:
    OS: macOS 15.7.2
    CPU: (8) arm64 Apple M1
    Memory: 70.19 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /usr/local/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 11.4.1 - /usr/local/bin/npm
    pnpm: 10.20.0 - /usr/local/bin/pnpm
  Browsers:
    Chrome: 142.0.7444.60
    Firefox: 144.0
    Safari: 18.6
    Safari Technology Preview: 26.0

Used Package Manager

npm

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

Contributions

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