1

When I setup Vite to load components using absolute path I get this MIME type not allowed console error.

vite.config.js:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
const path = require('path');

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: [{ find: '@', replacement: path.resolve(__dirname, '/src') }]
  }
});

jsconfig.json

{
  "compilerOptions": {
    "jsx": "preserve",
    "baseUrl": "./src",
    "paths": {
      "@/*": ["*"]
    }
  }
}

import:

import Container from '@/Components/UI/Container/Container';

console error

Any solution?

3
  • 1
    I cannot reproduce the problem in this demo. Can you share a link to a reproduction? Commented Oct 11, 2021 at 6:06
  • As we can't see it, he should also share the implementation of the <script></script> tag in the index.html file to be sure that he is not missing the type attribute from the script tag. Commented Oct 11, 2021 at 8:35
  • @tony19 I think the problem is with NodeJS because uploaded a test to GitHub and built it with Netlify and it worked. I'm getting the error only when I run dev on localhost. Commented Oct 11, 2021 at 13:57

1 Answer 1

2

The following configs worked for me. It is set for vueJs but also works well with react

In vite.config.js

import { defineConfig } from 'vite'

import react from '@vitejs/plugin-react'; import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
})

In jsconfig.json

 {
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

The configs are from here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.