Skip to content

Babel error immediately on eject #12070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sashafklein opened this issue Feb 16, 2022 · 17 comments · May be fixed by #12467
Open

Babel error immediately on eject #12070

sashafklein opened this issue Feb 16, 2022 · 17 comments · May be fixed by #12467

Comments

@sashafklein
Copy link

sashafklein commented Feb 16, 2022

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 lint script: 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:

image

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:

image

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.

@sashafklein sashafklein changed the title Babel error immediately on eject - 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. Babel error immediately on eject Feb 17, 2022
@tiny-james
Copy link

I found that the error could be stopped by changing the "eslintConfig" section in the package.json to add the NODE_ENV variable:

{
/*... snip ... */
"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "env": {
      "NODE_ENV": "development"
    }
  },
/*... snip ... */
}

So maybe eject needs to do that automatically?

@jacobweber
Copy link

jacobweber commented Mar 8, 2022

@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-app specifies:

    babelOptions: {
      presets: [require.resolve('babel-preset-react-app/prod')],
    },

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/index to be used instead. Eslint works if I delete this from package.json:

  "babel": {
    "presets": [
      "react-app"
    ]
  }

but that will probably mess with normal Babel compilation outside of eslint.

Not sure what the solution is.

@jacobweber
Copy link

OK, here's a hack that seems to work:

Update package.json to look like this:

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

This disables the "index" plugin when running eslint, so the "prod" one can be loaded.

@dingziqi
Copy link

OK, here's a hack that seems to work:

Update package.json to look like this:

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

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".

@jacobweber
Copy link

@dingziqi Hmm, not sure why it's working differently for you. I don't know if this would work, but you could try:

    "presets": [
        ["babel-preset-react-app", false],
        'babel-preset-react-app/prod'
    ]

nngthinh added a commit to nngthinh/react-photo-app that referenced this issue Mar 13, 2022
@lucasnogdias
Copy link

OK, here's a hack that seems to work:

Update package.json to look like this:

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

This disables the "index" plugin when running eslint, so the "prod" one can be loaded.

This worked for me, thank you! :)

@MuLoo
Copy link

MuLoo commented Apr 2, 2022

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 🙉

AlexanderKaluzhny added a commit to AlexanderKaluzhny/django-react that referenced this issue Apr 30, 2022
… requires that you specify `NODE_ENV` or `BABEL_ENV` environment variables."

Solution from: facebook/create-react-app#12070 (comment)
@kenbanks-peng
Copy link

kenbanks-peng commented May 16, 2022

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 :(

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

dwwinters pushed a commit to ccsm-cds-tools/ccsm-cds-dashboard that referenced this issue May 31, 2022
@draculapile
Copy link

Hi, I have met the same bug, and here are my steps to try to figure out this.

npx create-react-app my-app
cd my-app
npm run eject

Then, I created a launch.json in VSCode to debug:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug CRA ESLint",
      "type": "node",
      "request": "launch",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "program": "${workspaceFolder}/node_modules/eslint/bin/eslint.js",
      "args": [
        "--debug",
        "--ext",
        ".js",
        "${workspaceFolder}/src"
      ],
      "cwd": "${workspaceRoot}",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen"
    }
  ]
}

And, set a breakpoint in node_modules/babel-preset-react-app/create.js :

image

Pressed F5 and I got this:

image

So in my opinion, here is the reason:

once you run eject , CRA sets the eslintConfig and the babel fields in the package.json :

"eslintConfig": {
  "extends": [
    "react-app",
     "react-app/jest"
   ]
 },
"babel": {
  "presets": [
      "react-app"
    ]
}

ESLint will extend rules from create-react-app/packages/eslint-config-react-app , we can see it uses @babel/ellint-parser as parser:

// eslint-config-react-app/base.js
parserOptions: {
  sourceType: 'module',
  requireConfigFile: false,
  babelOptions: {
    presets: [require.resolve('babel-preset-react-app/prod')],
  },
},

therefore, when the breakpoint is executed to node_modules/@babel/core/lib/config/config-chain.js , there are two presets here:

image

one is from the "babel" field in the package.json file, and the other is from the babelOptions above

in the babel-preset-react-app/index.js, when the algorithm got here, env is undefined:

image

I think that's why we got the Error here. Besides, CRA always set env when we run start build and test, like this:

image

Also, you can reproduce this Error through a simple package.json file like this:

"devDependencies": {
  "@babel/core": "^7.18.2",
  "babel-preset-react-app": "^10.0.1",
  "eslint": "^8.17.0",
  "eslint-config-react-app": "^7.0.1"
},
"eslintConfig": {
  "extends": [
    "react-app"
  ]
},
"babel": {
  "presets": [
    "react-app"
  ]
}

As all of above, considering when we run the eject command, we tend to get a customized configuration(include env), it seems to be a good choice to set a default env in the babel-preset-react-app/index.js.

So I have opened a pull request #12467, and, is there a better way? : ) : ) : ) : ) : ) : )

windmeup added a commit to windmeup/dragonir-3d that referenced this issue Jul 2, 2022
@cat-kun
Copy link

cat-kun commented Jul 14, 2022

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".

"presets": [
  ["babel-preset-react-app", false],
  "babel-preset-react-app/prod"
]

windmeup added a commit to windmeup/dragonir-3d that referenced this issue Jul 21, 2022
@lushdog
Copy link

lushdog commented Sep 6, 2022

Add .eslintrc.js file and remove eslintConfig in package.json .

const NODE_ENV = process.env.NODE_ENV

module.exports = {
  extends: ['react-app'],
  rules: {},
  env: {
    NODE_ENV
  }
}

cmil added a commit to dracor-org/einakter that referenced this issue Sep 28, 2022
This adopts a solution suggested for
facebook/create-react-app#12070 which may not
be the best thing to do.

The original problem occured in VSCode for .js files only, e.g.:

Parsing error: [BABEL] /Users/cmil/Projects/dracor/einakter/tailwind.config.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/cmil/Projects/dracor/einakter/node_modules/babel-preset-react-app/index.js")
@scalybur
Copy link

I found that the error could be stopped by changing the "eslintConfig" section in the package.json to add the NODE_ENV variable:

{
/*... snip ... */
"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "env": {
      "NODE_ENV": "development"
    }
  },
/*... snip ... */
}

So maybe eject needs to do that automatically?

Works after ejection

@h2oearth
Copy link

It seems like the bug is still open :-/. I can bypass this issue by setting export NODE_ENV="development" in the console before opening VS Code. Waiting for a better option!

@ArpanSolanki29
Copy link

The following hack worked for me:

In the node_modules/babel-preset-react-app/index.js
replace line 18 with ==> const env = "development";

Restart the ESLint Server. (Do Ctrl+Shift+P and type "Restart ESLint Server").

Heydo referenced this issue in Heydo/react-learn-project Jun 28, 2023
项目报错 Using `babel-preset-react-app` requires that you specify `NODE_ENV` or `BABEL_ENV` 报错解决
@chengchaoyue
Copy link

@dingziqi Hmm, not sure why it's working differently for you. I don't know if this would work, but you could try:

    "presets": [
        ["babel-preset-react-app", false],
        'babel-preset-react-app/prod'
    ]

can solve problems

@byte-eth
Copy link

@dingziqi嗯,不确定为什么它对你来说工作方式不同。我不知道这是否会起作用,但你可以尝试:

    "presets": [
        ["babel-preset-react-app", false],
        'babel-preset-react-app/prod'
    ]

it's work well!thank you very much~

xx7y7xx pushed a commit to xx7y7xx/kepler.gl-examples that referenced this issue Oct 11, 2024
Failed to compile.
[eslint] package.json:
        Environment key "NODE_ENV" is unknown
ERROR in [eslint] package.json:
        Environment key "NODE_ENV" is unknown
webpack compiled with 1 error
Ref: facebook/create-react-app#12070 (comment)
@hubaia
Copy link

hubaia commented Oct 19, 2024

I found that the error could be stopped by changing the "eslintConfig" section in the package.json to add the NODE_ENV variable:

{
/*... snip ... */
"eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ],
    "env": {
      "NODE_ENV": "development"
    }
  },
/*... snip ... */
}

So maybe eject needs to do that automatically?

It's now 2024, and this issue still persists. Your solution remains valid, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.