[英]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. 我的package.json脚本块中有以下脚本,这需要通过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"
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 . 我尝试了许多组合的卸载,重新安装,更改脚本中要求的顺序以及此错误的主要git问题中提到的其他几个步骤。
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
. 当前,您使用
set
来指定NODE_ENV
变量,这将导致该变量不可用于子进程,即,在&&
运算符之后链接的下一个命令,即mocha
。
If you use export
instead, you'll ensure it's available to child processes. 如果改用
export
,则将确保它对子进程可用。
Change the beginning of your test
script to the following: 将
test
脚本的开头更改为以下内容:
"test": "export NODE_ENV=development && mocha --watch ..."
Cross-patform: 跨平台:
export
will only work in Bash environments. export
仅在Bash环境中有效。 For a cross-platform solution consider utilizing cross-env to set the environment variables instead. 对于跨平台解决方案,请考虑利用cross-env设置环境变量。 Example:
例:
"test": "cross-env NODE_ENV=development mocha --watch ..."
Note: export
has been replaced with cross-env
and the &&
operator is not necessary. 注意:
export
已由cross-env
替换,并且&&
运算符不是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.