vite icon indicating copy to clipboard operation
vite copied to clipboard

`*.json?url` imports behave inconsistently depending on import path

Open ds300 opened this issue 6 months ago • 1 comments

Describe the bug

When importing a .js file that in turn imports a .json file with the ?url suffix , Vite resolves the asset URL differently depending on whether the import is via a bare package specifier or a relative path into node_modules.

Let's say I have a dependency in node_modules containing the file

node_modules/subpackage/a.js:

import a from "./a.json?url";
export default a;

and then i import it like

import a from "subpackage/a.js";

a will be the inline json object.

But if I import it like

import a from "./node_modules/subpackage/a.js";

a will be a string path, as expected.

I made a minimal repro here: https://github.com/tldraw/vite-6-import-failure-repro/tree/minimal-repro (note the branch name)

Solves https://github.com/tldraw/tldraw/issues/6313

Reproduction

https://github.com/tldraw/vite-6-import-failure-repro/tree/minimal-repro

Steps to reproduce

Run git checkout minimal-repro && npm install && npm run dev

System Info

System:
    OS: macOS 15.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 111.73 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.16.0 - ~/.nvm/versions/node/v22.16.0/bin/node
    Yarn: 3.5.0 - ~/.nvm/versions/node/v22.16.0/bin/yarn
    npm: 10.9.2 - ~/.nvm/versions/node/v22.16.0/bin/npm
    pnpm: 9.15.4 - ~/.nvm/versions/node/v22.16.0/bin/pnpm
    bun: 1.2.7 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 137.0.7151.120
    Safari: 18.4
  npmPackages:
    vite: ^6.3.5 => 6.3.5

Used Package Manager

npm

Logs

No response

Validations

ds300 avatar Jun 24 '25 14:06 ds300

You can do something like this:

export default defineConfig({
  optimizeDeps: {
    exclude: ['subpackage']
  }
})

https://vite.dev/config/dep-optimization-options.html

brc-dd avatar Jun 25 '25 03:06 brc-dd

Related to #10838

wojtekmaj avatar Jun 26 '25 09:06 wojtekmaj