简体   繁体   中英

BABEL_ENV issue when running npm test in React App

I have the following script in the scripts block of my package.json, this requires babel via babel-register- and sets the NODE_ENV to development in order to do so.

"test": "set NODE_ENV=development&&mocha --watch --require babel-register --require jsdom-global/register --require ignore-styles --require src/tests/helpers.js src/tests/**/*.js"

I am however getting this error:

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.

I have tried many combinations of uninstalling, reinstalling, altering the order of requirements within my script and several other steps mentioned in the main git issue for this error .

Can anyone see any glaring error in my method here? Full package below:

"dependencies": {
  "prop-types": "^15.6.2",
  "react": "^16.4.1",
  "react-dom": "^16.4.1",
  "react-scripts": "1.1.4",
  "tslib": "^1.9.3",
  "typescript": "^3.0.1"
},
"scripts": {
  "build-css": "node-sass-chokidar src/ -o src/",
  "watch-css": "node-sass-chokidar src/ -o src/ --watch --recursive",
  "start-js": "react-scripts start",
  "build-ts": "set NODE_ENV=development tsc || exit 0",
  "watch-ts": "tsc --watch",
  "build": "react-scripts build",
  "start": "npm-run-all -p build-* watch-* start-js",
  "test": "set NODE_ENV=development&&mocha --watch --require babel-register --require jsdom-global/register --require ignore-styles --require src/tests/helpers.js src/tests/**/*.js",
  "eject": "react-scripts eject"
},
"babel": {
  "presets": [
    "es2015",
    "react",
    "react-app"
  ],
  "plugins": [
    "transform-object-rest-spread"
  ]
},
"devDependencies": {
  "babel-cli": "^6.26.0",
  "babel-plugin-transform-object-rest-spread": "^6.26.0",
  "babel-preset-es2015": "^6.24.1",
  "babel-preset-stage-2": "^6.24.1",
  "chai": "^4.1.2",
  "enzyme": "^3.4.1",
  "enzyme-adapter-react-16": "^1.2.0",
  "ignore-styles": "^5.0.1",
  "jsdom": "^11.12.0",
  "jsdom-global": "^3.0.2",
  "mocha": "^5.2.0",
  "node-sass-chokidar": "^1.3.3",
  "npm-run-all": "^4.1.3",
  "sinon": "^6.1.5"
}

You're currently using set to specify your NODE_ENV variable which results in it not being available to child processes, ie the next command chained after the && operator, which is mocha .

If you use export instead, you'll ensure it's available to child processes.

Change the beginning of your test script to the following:

"test": "export NODE_ENV=development && mocha --watch ..."

Cross-patform:

export will only work in Bash environments. For a cross-platform solution consider utilizing cross-env to set the environment variables instead. Example:

"test": "cross-env NODE_ENV=development mocha --watch ..."

Note: export has been replaced with cross-env and the && operator is not necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM