在React App中运行npm测试时出现BABEL_ENV问题

时间:2018-08-14 21:08:32

标签: reactjs npm babeljs create-react-app npm-scripts

在package.json的scripts块中有以下脚本,这需要通过babel-register-进行babel,并将NODE_ENV设置为development。

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

但是我收到此错误:

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.

我尝试了许多卸载,重新安装,更改脚本中需求顺序的组合以及the main git issue for this error中提到的其他几个步骤。

有人可以在我的方法中看到任何明显的错误吗?完整的包装如下:

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

1 个答案:

答案 0 :(得分:2)

您当前正在使用set来指定NODE_ENV变量,这将导致该变量不可用于子进程,即,在&&运算符之后链接的下一个命令,即{ {1}}。

如果改用export,则将确保它对子进程可用。

mocha脚本的开头更改为以下内容:

test

跨平台

"test": "export NODE_ENV=development && mocha --watch ..." 仅适用于Bash环境。对于跨平台解决方案,请考虑使用cross-env来设置环境变量。示例:

export

注意:"test": "cross-env NODE_ENV=development mocha --watch ..." 已替换为export,并且不需要cross-env运算符。