<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jessica Castro</title>
    <description>The latest articles on Forem by Jessica Castro (@jessicacastro).</description>
    <link>https://forem.com/jessicacastro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F326642%2F36b56adb-c54f-4901-8d83-8c5877309b17.png</url>
      <title>Forem: Jessica Castro</title>
      <link>https://forem.com/jessicacastro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jessicacastro"/>
    <language>en</language>
    <item>
      <title>How to configure aliases on React TS project with Vite</title>
      <dc:creator>Jessica Castro</dc:creator>
      <pubDate>Thu, 01 Jun 2023 00:14:10 +0000</pubDate>
      <link>https://forem.com/jessicacastro/how-to-configure-aliases-on-react-ts-project-with-vite-1n2g</link>
      <guid>https://forem.com/jessicacastro/how-to-configure-aliases-on-react-ts-project-with-vite-1n2g</guid>
      <description>&lt;p&gt;So, I usually like to use aliases in my projects, not only because it is easier to read and develop, it avoids breaking imports but also because we generate less CO2 digital footprints with fewer characters. &lt;/p&gt;

&lt;p&gt;As I started using &lt;strong&gt;Vite&lt;/strong&gt; in my personal projects, I started researching how to do it and ended up learning some details that I'll leave here for anyone who might be interested.&lt;/p&gt;

&lt;p&gt;If you don't know what are aliases, here is a resume:&lt;br&gt;
Everyday on dev life you have to import files to another and, if you are working on a big project, you see the paths for the import like this: &lt;code&gt;../../../../components&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is awful, right? With aliases you can resolve this problem and, basically, transform this: &lt;code&gt;../../../../componets&lt;/code&gt; into: &lt;code&gt;@/components&lt;/code&gt; or &lt;code&gt;@components&lt;/code&gt;. And in this post, I will show how to do it, with some explanation.&lt;/p&gt;
&lt;h2&gt;
  
  
  First of all, how to use path?
&lt;/h2&gt;

&lt;p&gt;Like you must know, the types of &lt;strong&gt;Node&lt;/strong&gt; aren't preinstalled on React with Typescript projects, but, you have to use the &lt;code&gt;path&lt;/code&gt; package on vite config later and you have to add these types for your project. So, first of all, make sure to run the command: &lt;code&gt;npm install -D @types/node&lt;/code&gt; on your terminal.&lt;/p&gt;

&lt;p&gt;For those who don't know what &lt;code&gt;-D&lt;/code&gt; means, it's basically install the package &lt;code&gt;@types/node&lt;/code&gt; as a development dependency. These dependencies aren't used on production environment, only when you are coding.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's configure our Typescript!
&lt;/h2&gt;

&lt;p&gt;For the Typescript understand that you want use aliases, you have to make sure you defined the &lt;code&gt;baseUrl&lt;/code&gt; and the &lt;code&gt;paths&lt;/code&gt; on your json configuration file (&lt;code&gt;tsconfig.json&lt;/code&gt;), inside your &lt;code&gt;compilerOptions&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;/*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;Aliases&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;*/&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"baseUrl"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./src"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"paths"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@components/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./components/*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@pages/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./pages/*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@assets/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./assets/*"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@styles/*"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"./styles/*"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These lines basically tells to your &lt;strong&gt;Typescript&lt;/strong&gt; where to find the base for the relative paths to configure each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  It's Vite time, baby!
&lt;/h2&gt;

&lt;p&gt;Even if you configure &lt;code&gt;path&lt;/code&gt; and &lt;code&gt;tsconfig.json&lt;/code&gt;, this still not work, why? Well, you have to tell &lt;strong&gt;Vite&lt;/strong&gt; to resolve your aliases too, otherwhise, it will not work, because &lt;strong&gt;Vite&lt;/strong&gt; don't know what you're doing on &lt;strong&gt;Typescript&lt;/strong&gt; configuration&lt;/p&gt;

&lt;p&gt;You remember &lt;code&gt;path&lt;/code&gt; package that you enabled the use by installing the node types? Now it's the time to use it!&lt;/p&gt;

&lt;p&gt;So, on our &lt;code&gt;vite.config.ts&lt;/code&gt; file we have to add the &lt;code&gt;resolve&lt;/code&gt; key and, inside that, the &lt;code&gt;alias&lt;/code&gt; key to define theses aliases too. So, after the plugin key, we can add these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;export&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;defineConfig(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;plugins:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;react()&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;resolve:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;alias:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"@components"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;path.resolve(__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src/components"&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"@pages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;path.resolve(__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src/pages"&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"@assets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;path.resolve(__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src/assets"&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"@styles"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;path.resolve(__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src/styles"&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: In my case, I only have these four folders to import and export things, but, if you have a few more, you can add the amount of aliases you want! By the way, if you're not using Typescript, since the third version of &lt;code&gt;create-react-app&lt;/code&gt; you can do this on React with Javascript too, you just have to create a &lt;code&gt;jsconfig.json&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;I hope this helps! Thanks for the reading! You can go to code now, with aliases! Big byes!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note: if you would like to read this article on Portuguese, please &lt;a href="https://www.linkedin.com/pulse/configurando-aliases-em-projetos-com-react-typescript-jessica-castro-"&gt;access my linkedin blog&lt;/a&gt;!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>vite</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
