본문 바로가기

react

[react] 절대 경로 사용해서 import 하는 방법

 

Importing a Component | Create React App

This project setup supports ES6 modules thanks to webpack.

create-react-app.dev

 

프로젝트 가장 상위 폴더에 jsconfig.json 파일을 생성한다.

 

생성된 jsconfig.json 파일 안에 아래 코드를 넣어주면 끝!

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

 

import Button from './Button';

상대 경로로 import하던 것을

 

import Button from 'components/Button';

이제 절대 경로로 import 가능하다.

import Button from 'src/components/Button';

와 같음

 

장점은 상대 경로를 사용할 때보다 깔끔해져서 보기 편하다는 것!