LoginSignup
22
11

More than 3 years have passed since last update.

[React] importする際に絶対パスで書きたい

Last updated at Posted at 2020-10-12

Reactでコンポーネントをimportする時などに、いちいち相対パスで書くのは面倒。そんな時は、絶対パスで書けるように設定しておきましょう。

//こうじゃなくて。。。
import Button from '../../components/atom/Button'

//こう書きたい
import Button from 'components/atom/Button'

設定

簡単です。
Create React Appのドキュメントに書いてあります。

プロジェクトのルートに、 jsconfig.json 又は、 tsconfig.jsonを作成して以下を追記するのみです。

jsconfig.json又はtsconfig.json

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

これでいちいち相対パスで書かなくて済みます。

Create React App [Absolute Imports]

22
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
11