10

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'

1 Answer 1

12

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';
Sign up to request clarification or add additional context in comments.

2 Comments

I added a link to the documentation in your answer, feel free to improve/change my edit ;)
@kinduser Thank you. I've missing the absolute path search term :) This method don't need to configure babel or anymore; Awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.