Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving Absolute Import Conflicts

Say I have a local file called "util.ts". With absolute imports enabled, I'm able to call everywhere in my app.

import * as util from 'util'

Now say theres a package named util in node_modules. That will be imported instead. Is there anyway to specify that I explicitly want my module rather than the one in node modules?

like image 407
fny Avatar asked May 28 '26 14:05

fny


1 Answers

reference: https://github.com/facebook/create-react-app/issues/7795

Try

{
  "compilerOptions":  {
    ...    
    "baseUrl": "."
  }
}

and prefix your imports with "src/", as in "src/util".

I encountered the exact some problem with wanting to use a file in my src directory named 'util.ts'. However, it turns out that node modules always win in cases of a name collision. The github issue link above references one possible solution, change your tsconfig baseUrl to '.' and for your absolute imports, prefix them with 'src', so 'src/util'.

I suspect you can use tsconfig 'paths' to alias things or avoid having to type src in other instances, but I haven't explored that route much as I actually am ok with prefixing my absolute imports with 'src'.

Paths reference: https://www.typescriptlang.org/tsconfig#paths

like image 178
Raymi306 Avatar answered May 31 '26 06:05

Raymi306



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!