How can I solve the issue of 'failed to resolve import in '@/...'" vitest?
Asked Answered
N

5

21

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

Neurovascular answered 21/3, 2023 at 7:47 Comment(0)
N
28

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

resolve: {
  alias: [{ find: "@", replacement: resolve(__dirname, "./src") }]
}
Neurovascular answered 21/3, 2023 at 8:57 Comment(4)
Here is the reference link.Karyogamy
can not find resolve or __dirnameArginine
@AmirRezvani import {resolve} from 'node:path'Din
yea, i figure it out later. @jonathan-dumaineArginine
P
8

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/') 
  }]
}
Punt answered 12/7, 2023 at 12:3 Comment(0)
A
2

Using vite-tsconfig-paths worked for me. This way aliases defined in tsconfig.json are read automatically in vitest.config.ts.

  1. Install npm install vite-tsconfig-paths --save-dev

  2. Configure vitest.config.ts:

    import { defineConfig } from 'vitest/config';
    import { defineConfig as viteDefineConfig } from 'vite';
    import tsconfigPaths from 'vite-tsconfig-paths';
    
    export default defineConfig(
      viteDefineConfig({
        // ... other Vite configurations
        plugins: [tsconfigPaths()],
      }),
    );
    
Avis answered 4/5 at 16:39 Comment(1)
perfect. thanksLassa
C
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.

Copulative answered 3/8, 2023 at 16:35 Comment(0)
K
0

The previous solutions didnt work for me, this is actually the one that solved my problem:

import { defineConfig } from 'vite';
import path from "node:path";

export default defineConfig({
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
});
Kurrajong answered 13/6 at 0:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.