Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite)

Absolute Import is a great way to clean up your imports

Setting up absolute imports can be a lot of pain to search on the internet, I spend quite some time to make this work, so here is all of the setup I use to make it work, all in one blog.


This content originally appeared on DEV Community and was authored by Theodorus Clarence

Absolute Import is a great way to clean up your imports

Setting up absolute imports can be a lot of pain to search on the internet, I spend quite some time to make this work, so here is all of the setup I use to make it work, all in one blog.

The problem

This is the usual way of importing with .. operator to go back 1 folder:

import Nav from '../../components/Nav';

In larger projects, this could be a nightmare.

And this is the cleaner import after using absolute import and alias:

import Nav from '@/components/Nav';

You can also change the @ symbol to whatever you want. Neat right?

There are 4 react apps setup I found on the internet, and I summarized all of those for you.

  1. For Next.js Apps
  2. For Create React App using Craco
  3. For Create React App without using Craco (alias not available)
  4. For React + Vite

For Next.js

  1. Add this to root with the filename of jsconfig.json
{
  "compilerOptions": {
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@/*": ["*"],
      "@/components/*": ["components/*"],
      "@/styles/*": ["styles/*"]
      // add more folders here
    }
  },
  "exclude": ["node_modules", ".next"]
}

Or you can just use my Next.js & Tailwindcss starter template with all of the boilerplate set up.

For Create React App using Craco

Only available if using Craco, usually we use Craco when also using Tailwind CSS, so no extra setup.

  1. Add this to root with the filename of jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./src",
    "jsx": "preserve",
    "paths": {
      "@/*": ["./src/*"],
      "@/components/*": ["./components/*"],
      "@/pages/*": ["./pages/*"],
      "@/contexts/*": ["./contexts/*"],
      "@/routes/*": ["./routes/*"]
    }
  },
  "exclude": ["node_modules", "build"]
}

You need to update this file every time you create a new folder

  1. And in craco.config.js
const path = require('path');

module.exports = {
  // ...existing code
  webpack: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
};

For Create React App without using Craco

Create React App only supports absolute import, but no alias

  1. Add in jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

The absolute import will only remove the relative path, but won't add @/ alias.

For example:

import Button from 'components/Button';

For React Vite

  1. Add this to root with the filename of jsconfig.json
{
  "compilerOptions": {
    "jsx": "preserve",
    "baseUrl": "./src",
    "paths": {
      "@/components/*": ["./components/*"],
      "@/pages/*": ["./pages/*"],
      "@/routes/*": ["./routes/*"],
    },
  },
}

You need to update this file every time you create a new folder

  1. Also add this to root with the filename of vite.config.js
import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
const path = require('path');

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: [{ find: '@', replacement: path.resolve(__dirname, '/src') }],
  },
  plugins: [reactRefresh()],
});

Or you can just use my Vite + React + Tailwindcss starter template

Originally posted on my personal site, find more blog posts and code snippets library I put up for easy access on my site ?


This content originally appeared on DEV Community and was authored by Theodorus Clarence


Print Share Comment Cite Upload Translate Updates
APA

Theodorus Clarence | Sciencx (2021-06-18T05:02:18+00:00) Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite). Retrieved from https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/

MLA
" » Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite)." Theodorus Clarence | Sciencx - Friday June 18, 2021, https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/
HARVARD
Theodorus Clarence | Sciencx Friday June 18, 2021 » Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite)., viewed ,<https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/>
VANCOUVER
Theodorus Clarence | Sciencx - » Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/
CHICAGO
" » Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite)." Theodorus Clarence | Sciencx - Accessed . https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/
IEEE
" » Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite)." Theodorus Clarence | Sciencx [Online]. Available: https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/. [Accessed: ]
rf:citation
» Clean Up Your Imports using Absolute Imports & Alias on React apps (Next.js, CRA, and React-Vite) | Theodorus Clarence | Sciencx | https://www.scien.cx/2021/06/18/clean-up-your-imports-using-absolute-imports-alias-on-react-apps-next-js-cra-and-react-vite/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.