4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

create-react-app 3 で絶対パスインポート

Posted at

環境

.sh
❯ npx create-react-app --version
3.0.1

version 3.0以上の環境だとsrc/hoge/piyoのように絶対パスでインポートできません。
../../..のような地獄から逃れるためには以下の設定が必要です。

絶対パスインポートするために必要な設定

1 環境変数NODE_PATH を設定する

プロジェクト直下に.envを作成します。

.env
NODE_PATH="."

2 tsconfig.jsonを設定する

プロジェクト直下にpaths.jsonを作成します。

paths.json
{
  "compilerOptions": {
    "baseUrl": "."
  }
}

tsconfig.jsonの最後の2行を追記します。

tsconfig.json
{
  "compilerOptions": {
     ...
  },
  "include": ["src"],
  "extends": "./paths.json"
}

これでsrc/~のように絶対パスでimportできます。:clap:

4
0
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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?