Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prioritize VS Code Auto-Import modules from my local directories over node_modules? [duplicate]

Let's say in my React project (TS if that matters), I have a custom components module that exports a component called Button. I also have a third party library in node_modules that also exports a component called Button. Is there a way to tell VS Code to prioritize my own module over those in node_modules for auto-import - i.e. I want my own modules to show up at the top of the import suggestion list rather than the third party node_modules ones if I type JSX like <Button.

like image 885
Kevin Shi Avatar asked Jan 24 '26 10:01

Kevin Shi


1 Answers

Apparently (if I am understanding what I'm reading correctly), this is supposed to be how it works, and it regressed at some point, but got fixed (emphasis on "apparently"). See Prefer local auto-imports over those from external modules #42104 and Matt Bierner's last comment there:

I believe this was fixed at some point (possible a duplicate of microsoft/TypeScript#23598)

If that's the case, if you're still getting this issue, it could be a regression, in which case it might be appropriate to raise a new issue ticket (make sure to include version numbers for everything relevant, and provide instructions to reproduce the issue).


You might also be interested in this TypeScript feature-request issue ticket: Auto-import priority rules #51155, which is more broad and makes the following suggestion:

  • code found within the repo/module (preferring aliased paths if they exist, probably preferring "closer" imports over further away in the tree?)
  • code found in other repos/modules defined locally within a monorepo (again preferring aliases)
  • code found in explicit dependencies found in package.json
  • code found in node internals (ex: fs, path)

You can give it a thumbs up to show support for it and subscribe to it to get notified about discussion and progress. But please avoid making noisy comments like "+1" / "bump".

Someone commented in the issue ticket that you can change the order of suggestions from the builtin TypeScript Language Support extension for VS Code by using a TypeScript plugin, which they made a repo for here (I have no affiliation with that repo or person)


For your reference / learning purposes, I found those issue tickets by googling "github typescript OR vscode issues import suggestion order" and clicking through related issue tickets.

like image 171
starball Avatar answered Jan 27 '26 00:01

starball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!