In the env.d.ts file in the root folder, use three slashes to import the type declaration file in node_module. Use import.meta.env in main.ts. It prompts that ImportMetaEnv does not exist. What is the reason?

1 The type declaration has been written in the env.d.ts file

2 But in main.ts, the Vite-specific object import.meta.env used to export environment variables prompts that ImportMetaEnv does not exist. Why does this happen?

 3 The reason is that in the ts.config.json file, the include field does not include the env.d.ts file at the first level in the root folder, so that the env.d.ts file will not be found in the root folder. , so the prompt does not exist

4 solutions:

method 1

In the ts.config.json file, add "*.ts" to the array of the include field. This means that ts will find the env.d.ts file in the root folder, so that import.meta.env will not prompt that it does not exist. .

Method 2

In the ts.config.ts file, add "vite/client" to the array of the types field. This means that ts contains the type declaration file of vite/client, so import.meta.env in main.ts will not prompt that it does not exist. .

NOTE: The final solution is

Because we created the VITE_HOME environment variable ourselves in the .env.development file in the root folder, we hope to have a smart prompt for the VITE_HOME we created in the import.meta.env of main.ts.

At this time, only by adding "*.ts" to the array of the include field will there be smart prompts . The above method 2 will not use smart prompts, only the 5 provided natively.

When vue3 introduces .vue files and .ts files, it prompts that the module cannot be found_"The module "vue-router" cannot be found. Do you mean to change the \"moduleresolution\" option_Melody's Volcano's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/Frank_colo/article/details/133276287