5

This is the error I got. There is a problem with the file path I defined in the "vite.config.ts" file. Can you help me?

Error Log

Error Message:

FAIL  tests/utils/ConvertFromDomainToCountryCode.test.ts [ tests/utils/ConvertFromDomainToCountryCode.test.ts ]
Error: Failed to resolve import "@/constant" from "src/assets/ts/_utils/ConvertFromDomainToCountryCode.ts". Does the file exist?

ConvertFromDomainToCountryCode.test.ts file

import { describe, expect } from "vitest";
import { SetFlags } from "../../src/assets/ts/_utils/ConvertFromDomainToCountryCode.ts";

describe("Convert From Domain To CountryCode", () => {
  test("function defined", () => {
    expect(SetFlags).toBeDefined();
  });
});

Here it works fine when I make the file path "../../../constant/index".

ConvertFromDomainToCountryCode.ts file

import { COUNTRY_INFO } from "@/constant";

Here i added "alias"

vite.config.ts file

import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve, dirname } from "node:path";
import vueJsx from "@vitejs/plugin-vue-jsx";

export default defineConfig({
  plugins: [
    vue(),
    vueJsx()
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url))
    }
  },
  base: "/"
});

System

OS: macOS 13.2.1
CPU: (8) arm64 Apple M1 Pro
Memory: 91.50 MB / 16.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 16.18.1 - ~/.nvm/versions/node/v16.18.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v16.18.1/bin/yarn
npm: 8.19.2 - ~/.nvm/versions/node/v16.18.1/bin/npm
Browsers:
Chrome: 111.0.5563.64
Safari: 16.3
npmPackages:
@vitejs/plugin-vue: ^3.1.2 => 3.2.0
@vitejs/plugin-vue-jsx: ^3.0.0 => 3.0.0
@vitest/coverage-istanbul: ^0.29.3 => 0.29.3
vite: ^3.1.8 => 3.2.5
vitest: ^0.29.3 => 0.29.3

Just waiting for the path of the file to be found

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
mahmutoz
  • 131
  • 1
  • 7

3 Answers3

8

I solve this error. I add this code in vitest.config.ts file.

resolve: {
  alias: [{ find: "@", replacement: resolve(__dirname, "./src") }]
}
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
mahmutoz
  • 131
  • 1
  • 7
3

Had the same problem and the answer helps to solve it but I wanna add you can import resolve from path

import { resolve } from 'path'

and if you haven't declared @ in your tsconfig but you have dynamic path for some specific folders you can just specify them.

resolve: {
  alias: [{ 
    find: "@server", 
    replacement: resolve(__dirname, './src/server/') 
  }]
}
Repu
  • 31
  • 2
0

I encountered an error when trying to use material icons with the default import provided by Material UI icons (v5) on my code that use material ui 4. The code I used was:

import CheckCircleIcon from '@mui/icons-material/CheckCircle';

The error I received was:

[plugin:vite:import-analysis] Failed to resolve import "@mui/icons-material/CheckCircle" from "src/auth/components/Pricing/PricingTable.tsx". Does the file exist?

Since I am using Material UI version 4, I needed to modify the import to:

import CheckCircleIcon from "@material-ui/icons/CheckCircle";

After making this change, the problem was resolved, and the component worked perfectly.

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74