9

Is there a babel plugin to avoid long import path in CRA? I've searching a lot on the web but I can't find the best way to achieve this.

Actual:

import MyComponent from '../../../../components/MyComponent'

Expected

import MyComponent from 'components/MyComponent'
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Tolotra Raharison
  • 3,034
  • 1
  • 10
  • 15

1 Answers1

11

In your main root, create file jsconfig.json:

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

Where src is the folder where you store your project files, sometimes it may be /app or /src.

Then you will be able to import your components with an absolute path:

import MyComponent from 'components/MyComponent';
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
kind user
  • 40,029
  • 7
  • 67
  • 77