create-react-app: Babel error immediately on eject
Describe the bug
If I start a completely fresh CRA install, and then simply eject it:
- My text editor (VSCode) throws an error on the first line of every JS file.
- Running eslint (by adding a
lintscript:eslint .) throws the same error for every JS file.
That error is:
Parsing error: [BABEL] /Users/sasha/.../App.js: Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables. Valid values are "development", "test", and "production". Instead, received: undefined. (While processing: "/Users/sasha/.../node_modules/babel-preset-react-app/index.js")
Did you try recovering your dependencies?
I did not, because I got this working on a completely fresh install (as an attempt to reproduce the problem on another repo where I’ve tried recovering my dependencies multiple times).
Which terms did you search for in User Guide?
I searched on Google and found this previous issue, as well as several SO posts that didn’t help. The solution proposed in the linked issue has already been applied to CRA, so that doesn’t seem to be the issue.
Environment
$ npx create-react-app --info
npx: installed 67 in 3.055s
Environment Info:
current version of create-react-app: 5.0.0
running from /Users/sasha/.npm/_npx/9320/lib/node_modules/create-react-app
System:
OS: macOS 12.0.1
CPU: (10) x64 Apple M1 Pro
Binaries:
Node: 14.16.0 - ~/.asdf/installs/nodejs/14.16.0/bin/node
Yarn: 1.22.10 - ~/.asdf/installs/nodejs/14.16.0/.npm/bin/yarn
npm: 6.14.11 - ~/.asdf/installs/nodejs/14.16.0/bin/npm
Browsers:
Chrome: 98.0.4758.102
Edge: Not Found
Firefox: 96.0.2
Safari: 15.1
npmPackages:
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
react-scripts: Not Found
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
Set up a new app:
npx create-react-app demo
cd demo
Add a lint command to package.json:
"lint": "eslint ."
Validate that it runs without error:
npm run lint
Validate that the text editor (in my case VSCode) detects no issues with JS files by opening one, and seeing no error tilde at the top of the file:
Then, eject, and watch everything break:
npm run eject
npm run lint # throws many of the above error
Look at a file in VSCode, and it will also show the error tilde at the top of the file. On hover, it shows the aforementioned error:
If I return to the pre-ejected state (get rid of all eject-changes and then npm install), the issues go away.
Expected behavior
An immediately ejected app behaves identically to a non-ejected one. In particular, it doesn’t break Babel parsing and eslint.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 30
- Comments: 17
Commits related to this issue
- fix: specify env after ejecting the default config More in https://github.com/facebook/create-react-app/issues/12070 — committed to nngthinh/react-photo-app by nngthinh 5 years ago
- fix for Eslint "Parsing error: [BABEL] Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables." Solution from: https://github.com/facebook/create-reac... — committed to AlexanderKaluzhny/django-react by AlexanderKaluzhny 5 years ago
- Updating config to address this issue: https://github.com/facebook/create-react-app/issues/12070 — committed to ccsm-cds-tools/ccsm-cds-dashboard by deleted user 4 years ago
- https://github.com/facebook/create-react-app/issues/12070 — committed to windmeup/dragonir-3d by windmeup 4 years ago
- Revert "https://github.com/facebook/create-react-app/issues/12070" This reverts commit f6f857fcbd08d888e17ff6cd3e66209f3fc8a69e. — committed to windmeup/dragonir-3d by windmeup 4 years ago
- Silence eslint complaining about missing BABEL_ENV This adopts a solution suggested for https://github.com/facebook/create-react-app/issues/12070 which may not be the best thing to do. The original ... — committed to dracor-org/einakter by cmil 4 years ago
- fix: problem with eslint and babel https://stackoverflow.com/questions/72249976/after-npm-eject-in-cra-environment-variables-receive-undefined https://github.com/facebook/create-react-app/issues/1207... — committed to h1karxii/react-stylint-methods by h1karxii 4 years ago
- eject以后的报错信息(不影响项目运行) 项目报错 Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` 报错解决 — committed to Heydo/react-learn-project by Heydo 3 years ago
- Fix npm start error: Failed to compile. [eslint] package.json: Environment key "NODE_ENV" is unknown ERROR in [eslint] package.json: Environment key "NODE_ENV" is unknown webpack compi... — committed to xx7y7xx/kepler.gl-examples by devin-grabtaxi 2 years ago
I found that the error could be stopped by changing the
"eslintConfig"section in thepackage.jsonto add theNODE_ENVvariable:So maybe eject needs to do that automatically?
@dingziqi Hmm, not sure why it’s working differently for you. I don’t know if this would work, but you could try:
OK, here’s a hack that seems to work:
Update package.json to look like this:
This disables the “index” plugin when running eslint, so the “prod” one can be loaded.
In my case, a new Error will be throwed:Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): “jsx”, “flow”, “typescript”.
Hoping this is fixed in the eject process. Strangely but not unexpectedly, this enhanced hack does not work for me, so eslint gives me the error for every file it assesses in my project 😦
Hi, I have met the same bug, and here are my steps to try to figure out this.
Then, I created a
launch.jsonin VSCode to debug:And, set a breakpoint in
node_modules/babel-preset-react-app/create.js:Pressed F5 and I got this:
So in my opinion, here is the reason:
once you run
eject, CRA sets theeslintConfigand thebabelfields in thepackage.json:ESLint will extend rules from
create-react-app/packages/eslint-config-react-app, we can see it uses@babel/ellint-parseras parser:therefore, when the breakpoint is executed to
node_modules/@babel/core/lib/config/config-chain.js, there are two presets here:one is from the
"babel"field in thepackage.jsonfile, and the other is from thebabelOptionsabovein the
babel-preset-react-app/index.js, when the algorithm got here,envisundefined:I think that’s why we got the
Errorhere. Besides, CRA always set env when we runstartbuildandtest, like this:Also, you can reproduce this
Errorthrough a simplepackage.jsonfile like this:As all of above, considering when we run the
ejectcommand, we tend to get a customized configuration(include env), it seems to be a good choice to set a defaultenvin thebabel-preset-react-app/index.js.So I have opened a pull request #12467, and, is there a better way? : ) : ) : ) : ) : ) : )
Add .eslintrc.js file and remove eslintConfig in package.json .
Works after ejection
add
"parserOptions": { "babelOptions": { "presets": [ ["babel-preset-react-app", false] ] } }to your eslintrc.js or package.json and still not work ? maybe you should try restart your vscode 🙉@tiny-james If I do that, I just get an error from eslint, “Environment key “NODE_ENV” is unknown”. I think it may just be stopping eslint from working.
The underlying issue seems to be that, although
eslint-config-react-appspecifies:and that file does get loaded, its default export is never called when running eslint. Instead,
babel-preset-react-app/index’s default export is called.It looks like the babel presets in package.json cause
babel-preset-react-app/indexto be used instead. Eslint works if I delete this from package.json:but that will probably mess with normal Babel compilation outside of eslint.
Not sure what the solution is.