question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@/ imports - consider alternatives?

See original GitHub issue

Thank you for this amazing source of practices, I’ve been sharing it with lot of people!

@/ imports confuses me, could you please add alternatives to the doc? I’m not sure if and why this is a best way.

Another option is to set baseUrl: 'src' to tsconfig and now instead of ‘@/components/X’ we import from ‘components/X’. I don’t think there are meaningful and maintained ‘components’, ‘hooks’, ‘lib’, ‘assets’ packages on npm.

Another option is to use relative paths, editors are smart nowadays, they write imports automatically, they update imports automatically when moving files. At least VSCode and JetBrains editors a capable. Most of people are using VSCode, so this shouldn’t be a problem.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
alan2207commented, Jan 11, 2022

It works well with VSCode, you should definitely give it a shot.

And if you have the import/order enabled in eslint you will get a nice visual separation of local vs dependency modules which you do not get with the suggested approach.

Screenshot 2022-01-11 at 09 32 30

(ignore the squiggly line in the left image for now), VSCode did linting auto fix on save automatically and reordered the imports.

2reactions
alan2207commented, Jan 10, 2022

Hi there,

Both of your suggestions are valid. I wrote a bit here about the reason why I am choosing this way:

“It is also possible to define multiple paths for various folders(such as @components, @hooks, etc.), but using @/* works very well because it is short enough so there is no need to configure multiple paths and it differs from other dependency modules so there is no confusion in what comes from node_modules and what is our source folder. That means that anything in the src folder can be accessed via @, e.g some file that lives in src/components/MyComponent can be accessed using @/components/MyComponents.”

In a nutshell, I find it useful to have a symbol in front of it because it will prevent conflicts from node_modules imports in case a folder in src has the same name as some dependency - happens not very often, but it’s nice to be sure and not to think about it ever again. In the end, it’s just a matter of preference 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Import substitution is making an unwelcome comeback | PIIE
Import substitution is the idea that blocking imports of manufactured goods can help an economy by increasing the demand for domestically ...
Read more >
Import: Definition, Examples, and Pros and Cons - Investopedia
An import is a good or service bought in one country that was produced in another. Imports and exports are the components of...
Read more >
Why Alternatives To Tariffs Can Be More Effective - NPR
Tariffs are a tax on imports, which is supposed to make imports more expensive and domestic goods at least relatively more appealing, right?...
Read more >
A Secret Weapon for Doing Competitor and Supplier Research
If you've been importing long enough, you've probably found a competitor (probably on Amazon) selling the exact same product as you, but at...
Read more >
Alternatives to Food Import Dependency - Global Policy Forum
There is a vast number of proposals, strategies and initiatives how to improve food security and agricultural production.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found