Skip to content

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

Open
@ds300

Description

@ds300

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 tldraw/tldraw#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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions