DEV Community

Cover image for Absolute imports in Create React App
Michał Bednarz
Michał Bednarz

Posted on • Edited on

Absolute imports in Create React App

One of the things that held me back from Create React App was the lack of absolute imports. I loved them so much in my custom webpack config that I couldn't live without it. After a time I found that they can be configured in CRA too! Let's see how to do it.

Why do you need absolute imports?

During the development of the React app, you may encounter a situation like on the code below. Multiple, relative, nested imports. It's so hard to manage! And things are even worse when it comes to moving your code around a file structure.



import React, { Fragment } from 'react';
import PropTypes from 'prop-types';

import Icon from './../../components/icon';
import AuthorAvatar from './../../components/author-avatar';
import Date from './../../components/date';
import Image from './../../components/image';
import HtmlLink from './../../components/html-link';
import { SOURCES } from './../../config/defaultConfig';

import * as styles from './index.scss';


Enter fullscreen mode Exit fullscreen mode

Absolute imports to the rescue!

Absolute imports can help you make that code a lot cleaner, more readable and manageable. We want to be able to transform our paths from this:



import useApi from './../../hooks/use-api';
import Date from './../../components/date';
import Image from './../../components/image';
import transfromUserData from './../../helpers/transform-user-data';
// etc...


Enter fullscreen mode Exit fullscreen mode

to that:



import useApi from 'hooks/use-api';
import Date from 'components/date';
import Image from 'components/image';
import transfromUserData from 'helpers/transform-user-data';


Enter fullscreen mode Exit fullscreen mode

Let's assume you have src with multiple directories where your code lives. We want to make src your base path for the imports.

Example structure of your src

The first step is to create jsconfig.json file (if it doesn't exist already) in root directory of your CRA app (tsconfig.json in case you used TypeScript template). Then add baseUrl configuration to it:



{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}


Enter fullscreen mode Exit fullscreen mode

That few simple lines will tell CRA config to use your src directory as a base for your imports so you can import your components with absolute paths. After that you can change imports in your code as presented below:



import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import replace from 'lodash/replace';

import Icon from 'components/icon';
import AuthorAvatar from 'components/author-avatar';
import Date from 'components/date-representation';
import Image from 'components/image';
import HtmlLink from 'components/html-link';
import { SOURCES } from 'config';

import * as styles from './index.scss';


Enter fullscreen mode Exit fullscreen mode

A lot cleaner and without ./../ mess. I hope you found it right on time! :)

Docs reference: https://create-react-app.dev/docs/importing-a-component#absolute-imports

Top comments (21)

Collapse
 
dorklord23 profile image
Tri R.A. Wibowo

Weird. This doesn't work for me :/

Collapse
 
winkee01 profile image
winkee01

try restart react app?

Collapse
 
dorklord23 profile image
Tri R.A. Wibowo

it turned out I need to set "baseUrl": "./src" instead of "baseUrl": "src" in my case.

Collapse
 
mr_frontend profile image
Michał Bednarz

What's the error? Is it during bundling or in runtime?

Collapse
 
dorklord23 profile image
Tri R.A. Wibowo • Edited

No error log per se but the absolute import just didn't work so I need to resort to set NODE_PATH='src/' in .env to make it work which is already deprecated.

Collapse
 
uahmadsoft profile image
UAhmadSoft

I tried this , It worked ......But when I tried to deploy my app on netlify , It failed on Build process with error
Cannot find module: 'components/Footer'. Make sure this package is installed.

How to use fix this on netlify ?

Collapse
 
pszndr profile image
Paulo • Edited

This is great timing, I was just looking into how to do this and a solution that I tried using an .env file was not working, and this right here did the trick. Thanks a lot!

Collapse
 
mr_frontend profile image
Michał Bednarz

Love to hear that!

Collapse
 
nafronte profile image
htmlcode

Thanks a lot! Have you any tips in case if I want organize paths for styles structure? What should I do with imports in scss files?

Collapse
 
enzo profile image
Enzo

Awful solution by the React team. What if I have a dir called lodash. How do I know if is importing the npm package or my dir?

Collapse
 
nstanard profile image
Neal Stanard

In that case you would do "baseUrl": "./", and import from your dir using src/lodash

Collapse
 
ruanengelbrecht profile image
Ruan Engelbrecht

Legend!

Collapse
 
matias_hevich profile image
Matias Hevich

THANKS A LOT!

Collapse
 
tyroneweb profile image
薛清扬

nice! it's work

Collapse
 
imvsnu profile image
imvsnu

Thanks a lot.

Collapse
 
satishgupta9794 profile image
Satish gupta

Thank you @michal , I just looking for absolute path in app, Now its working.
at the place of "src" put "./src", in VS code. It will suggest path.