LoginSignup
0
0

More than 1 year has passed since last update.

create-react-appで作成したアプリをnpm run ejectするとエラーが発生する

Posted at

create-react-appで作成したアプリをnpm run ejectするとエラーが発生する。原因と解決方法

create-react-appで作成したアプリをnpm run ejectするとエラーが発生すると思います。

エラー↓です。

Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables. 」

この記事とこのエラーの発生原因と回避手段を紹介します。

「アプリ作成 → eject → エラー再現 → 解決」の流れで説明します。

①create-react-appで'react-app'のアプリを作成する(雛形作成)

image.png

create-react-app react-app

上記のように雛形作成できました。

②npm run ejectで設定ファイルなどを展開する

image.png

③vs-codeでフォルダを開く

何もせずにしばらく待つとエラーが表示されてしまう。
npm startで通常に起動できますが、やっぱり気持ちわるいよね。
image.png
image.png

④なぜ「Using babel-preset-react-app requires that you specify NODE_ENV or BABEL_ENV environment variables. 」エラーが発生しているのか

https://github.com/facebook/create-react-app/issues/12070
の検討内容を参照してください。
一言でいうとライブラリの不具合です。
https://github.com/facebook/create-react-app/pull/12467/commits/757a1b1b325d305dc4b721ea4f79ff25421fd65f
で解決できたと思います。
ライブラリの更新で解決できるかと思いますが、現時点の解決手段
として以下の内容をpackage.jsonに追加すること

  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "parserOptions": {
      "babelOptions": {
          "presets": [
             ["babel-preset-react-app", false],
             ["babel-preset-react-app/prod"]
          ]
      }
    }
  },

するとエラーが解消できました。
image.png

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