Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make VSCode resolve "@" in imports

I'm working in a Vue.js project that uses Webpack. Many imports use @ as an alias to src directory. How can VSCode be configured to resolve the alias and make intellisense work with those imports?

I have found in many places on the web how to setup aliases in VSCode, using jsconfig.json or tsconfig.json. I have tried configuring in both files and nothing seems to work.

My jsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "allowSyntheticDefaultImports": false,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "dist"]
}

I expected VSCode to resolve the alias in the path, making intellisense and peeking definition work.

like image 314
Gabriel Mineiro Avatar asked Jul 16 '19 17:07

Gabriel Mineiro


People also ask

How do you solve Pylance missing imports in VS Code?

If you are using the VS, Please go to the settings .. search for Advance path and then ADD it (/. source) it should solve the problem. Hope you have installed the Pylance in your system correctly.

Why import is not working in VS Code?

You may have installed the libraries inside a virtual environment and you are using a python interpreter that is not inside the environment. Edit: It could be that just one of your libraries is inside (or outside) the environment.

How do I organize imports in VS Code?

In VSCode, go to File -> Preferences -> Settings and click on the icon in the top right hand corner to open up the settings in JSON. Et voilà! Your imports will now be organized every time you save a file.


1 Answers

In my current project, I have a jsconfig.json at the root containing this :

{
  "compilerOptions": {
    "target": "es2017",
    "allowSyntheticDefaultImports": false,
    "baseUrl": "./",
    "paths": {
      "@/*": ["src/*"],
    }
  },
  "exclude": ["node_modules", "dist", "docs"]
}

and it works for me.

I don't know exactly what moduleResolution is doing in your conf, and my baseUrl is the root folder in my case.

The related doc for vs code : https://code.visualstudio.com/docs/languages/jsconfig#_using-webpack-aliases

Try it and tell me if it's working better ?

like image 143
Mathieu D. Avatar answered Oct 12 '22 23:10

Mathieu D.