Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8824

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T04:44:09+00:00 2024-11-28T04:44:09+00:00

Tired Of Relative Imports? Time To Get Rid Of Them!

  • 60k

At some point in your Javascript developer journey, you've certainly encountered these types of imports:

import Car from '../../../vehicles/car' import House from '../../../buildings/house' 
Enter fullscreen mode Exit fullscreen mode

And you were probably bothered by the ugliness of them…

https://media.giphy.com/media/3o8doVY3jacRLyrSmI/giphy.gif

But guess what? I'm here as a savior to show you how to get rid of them! (really) 😎

Ready? Let's go! 🏁

baseUrl

The simplest way to get rid of these awful imports is by simply editing your jsconfig.json file. If you don't already have one, you can just create it at the root of your project.

Let's complete it with the following keys:

{     "compilerOptions": {         "baseUrl": "."     } } 
Enter fullscreen mode Exit fullscreen mode

Now, the most studious ones of you might think: “Compiler options? But JavaScript is not a compiled language!”. And you're right! If you want to know why does this key exist, I recommend you to follow this link from the official website of Visual Studio Code.

Now, imagine having the following directory structure:

. β”œβ”€β”€ components β”‚   └── layouts β”‚       └── header.js β”œβ”€β”€ styles β”‚   └── header.css └── jsconfig.json 
Enter fullscreen mode Exit fullscreen mode

If you want to include your header.css style file in your header.js file, you can now do it this way:

// header.js import 'styles/header.css' 
Enter fullscreen mode Exit fullscreen mode

Without this configuration, here's how you would have done it:

// header.js import '../../styles/header.css' 
Enter fullscreen mode Exit fullscreen mode

Now, no matter how deep you are in your project's architecture, you'll be able to import your files as if you were at the root of your project. 😲

And obviously, you'll still able to import them relatively from the current directory you are in!

. β”œβ”€β”€ src β”‚   β”œβ”€β”€ vehicles β”‚   β”‚   └── car.js β”‚   β”‚   └── truck.js β”‚   └── index.js └── jsconfig.json 
Enter fullscreen mode Exit fullscreen mode

 // index.js import truck from './vehicles/truck.js' 
Enter fullscreen mode Exit fullscreen mode

https://media.giphy.com/media/oYtVHSxngR3lC/giphy.gif

Paths

Back to our jsconfig.json. You can also add the paths key in order to map an import to a specific folder. This is useful for giving aliases to some folders in your imports.

{     "compilerOptions": {         "baseUrl": ".",         "paths": {             "css/*": ["styles/*"]         }     } } 
Enter fullscreen mode Exit fullscreen mode

Considering the same folder structure we've seen in the previous part, you could now import your styles like this:

// header.js import 'css/header.css' 
Enter fullscreen mode Exit fullscreen mode

But I wouldn't recommend doing this at all, as this would create inconsistencies between the real folders' names and the aliases β€” instead, why not simply renaming the actual folder? 🀨

Nevertheless, this option can be useful for folders you often use and that are not at the root of your project. Let's consider the following structure:

. β”œβ”€β”€ assets β”‚   β”œβ”€β”€ styles β”‚   β”‚   └── index.css |── src β”‚   └── index.js └── jsconfig.json 
Enter fullscreen mode Exit fullscreen mode

We will often use the styles directory to import our styles, and that could be very handy if we could remove the assets prefix, in order to not have to always write this:

import 'assets/styles/index.css' 
Enter fullscreen mode Exit fullscreen mode

In that case, you could add the following to your jsconfig.json:

{     "compilerOptions": {         "baseUrl": ".",         "paths": {             "@styles/*": ["assets/styles/*"]         }     } } 
Enter fullscreen mode Exit fullscreen mode

After that, here's how you would import your styles:

import '@styles/index.css' 
Enter fullscreen mode Exit fullscreen mode

The @ is a conventional way to reference a folder-mapping import, in order to differentiate it from a classic import.

TypeScript

Oh, you are using TypeScript? Awesome! Of course you can also use this trick: the only difference is that you won't write those settings inside the jsconfig.json file, but instead, inside…?

https://media.giphy.com/media/3o7TKTDn976rzVgky4/giphy.gif

Exactly! Inside the tsconfig.json file. Smart, isn't it? 😏

Conclusion

That's it! You've now got rid of those awful imports, congratulations! πŸŽ‰

To go further, I would recommend you following this link from the official Visual Studio Code website, in particular to exclude some directories that are not part of the source code.

With that being said, I thank you for reading me all the way through, and I hope you've learned something with this article. 😎

https://media.giphy.com/media/3o6Zt6KHxJTbXCnSvu/giphy.gif

javascripttutorialtypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.