2

So I have started adding a jsconfig.json file to my react projects in VS Code to enable me to use imports like:

import { Home, Expenses, Invoices } from '@/views';

instead of:

import { Home, Expenses, Invoices } from '../views';

My jsconfig file looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "baseUrl": "src",
    "paths": {
      "@/*": ["/*"],
      "@/components/*": ["/components/*"],
      "@/views/*": ["/views/*"],
      "@/styles/*": ["/styles/*"]
    }
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

However one thing I have started to notice, is that whenever I do this I can no longer click on 'Home' say and be taken to the code/file. Instead it always brings up a popover that just shows references to the file. Nothing I click on within this reference popover will take me to the file. It looks like this:

popover example

Sometimes it doesn't even bring up the hover. It will just select the element in the open code block, if I click on the element it will just select the import at the top.

If I remove the '@' sign then everything works as intended.

Is there any way to use a jsconfig.json file with absolute imports/path alias' AND keep the ability to click on the import to go the file/code.

Hazy
  • 1,766
  • 2
  • 8
  • 12
  • When you remove the `@` sign and everything works, do you also remove the first `/`? I suspect that forward slash is the issue. Why no try aliases such as `@Views` and `@Components`? Personally, I would not have a general `@*` alias because it could lead to spaghetti imports which can be hard to maintain. Alias should (imo) be for common parent imports which are approved to be imported from anywhere (constants, analytics, etc) and nothing more. – Patrick Jun 13 '22 at 18:06

0 Answers0