Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aliases break JSON imports #10669

Closed
7 tasks done
amh4r opened this issue Oct 27, 2022 · 2 comments
Closed
7 tasks done

Aliases break JSON imports #10669

amh4r opened this issue Oct 27, 2022 · 2 comments
Labels
invalid This doesn't seem right

Comments

@amh4r
Copy link

amh4r commented Oct 27, 2022

Describe the bug

When importing a JSON file using an alias, the app doesn't load and the following error shows in the browser console:

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.

In other words, if you have the @ alias declared in vite.config.js:

resolve: {
  alias: {
    "@": "./src",
  },
},

And then import a JSON file using the alias:

import config from "@/config.json";

The error will happen.

This also happens when the aliased import is indirect. For example, if myFile.js imports config.json using a relative path but another file imports myFile.js using an alias.

Reproduction

https://github.com/goodoldneon/vite-alias-json-bug-repro

Steps to reproduce

  1. npm run dev
  2. Change import to import config from "@/anotherFile.js" in src/App.jsx
  3. Look at the page in the browser

System Info

  System:
    OS: macOS 12.6
    CPU: (10) arm64 Apple M1 Max
    Memory: 1.72 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.1 - ~/.nvm/versions/node/v16.17.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.1/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.1/bin/npm
  Browsers:
    Chrome: 106.0.5249.119
    Firefox: 105.0.3
    Safari: 16.0
  npmPackages:
    @vitejs/plugin-react: ^2.2.0 => 2.2.0
    vite: ^3.2.0 => 3.2.0

Used Package Manager

npm

Logs

No response

Validations

@sapphi-red
Copy link
Member

alias is a simple replace. So you need to change resolve.alias from

	'@': './src'

to

	'@': path.resolve(__dirname, './src')

.
https://vitejs.dev/config/shared-options.html#resolve-alias:~:text=When%20aliasing%20to%20file%20system%20paths%2C%20always%20use%20absolute%20paths.%20Relative%20alias%20values%20will%20be%20used%20as%2Dis%20and%20will%20not%20be%20resolved%20into%20file%20system%20paths.

@sapphi-red sapphi-red added invalid This doesn't seem right and removed pending triage labels Oct 28, 2022
@amh4r
Copy link
Author

amh4r commented Oct 31, 2022

I missed that in the docs! Thank you for pointing it out -- it works now

@github-actions github-actions bot locked and limited conversation to collaborators Nov 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants