babel-preset-react-app not picking up environment variables

拈花ヽ惹草 提交于 2021-01-22 08:15:23

问题


Failed to compile ./src/index.js Module build failed: Error: 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 preset:"C:\Users\mitch\OneDrive\Development\Git\react-seed\node_modules\babel-preset-react-app\index.js") at Array.map (native)

I keep getting the above error no matter how many weird and wonderful ways I try to set either the environment or the environment variables since updating my react-app and I even get this on a fresh app created from the 'create-react-app' scripts - What am I clearly doing wrong?


回答1:


I do it in my package.json:

{
  "scripts": {
    "build": "NODE_ENV=development babel src -d lib",
    "build-prod": "NODE_ENV=production babel src -d lib"
  }
}



回答2:


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: "development "

Ran into a similar problem and noticed that a space character messed things up with this configuration:

SET NODE_ENV=development && node server/bootstrap.js

//Changed to this:
SET NODE_ENV=development&&node server/bootstrap.js



回答3:


{
  "scripts": {
    "build": "export NODE_ENV=development && babel src -d lib",
    "build-prod": "export NODE_ENV=production && babel src -d lib"
  }
}



回答4:


Cross-Platform Compatibility

If you're aiming for cross platform compatibility, you should use cross-env

{
  "scripts": {
    "build:dev": "npx cross-env NODE_ENV=development babel src -d lib",
    "build:prod": "npx cross-env NODE_ENV=production babel src -d lib"
  }
}

The current answers will only work in windows or on mac, and the package.json should be shippable across multiple clients without having to customize too much for each os

See also this walkthrough (mine) of Making NPM scripts work cross platform



来源:https://stackoverflow.com/questions/44198100/babel-preset-react-app-not-picking-up-environment-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!