简体   繁体   English

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

[英]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.

相关问题 在 React 应用程序中运行 npm start 时出现 babel-jest 依赖问题 - Issue with babel-jest dependency when running npm start in a React app Husky 错误 'BABEL_ENV' 不被识别为内部或外部命令, - Husky error 'BABEL_ENV' is not recognized as an internal or external command, 在 React 和 Webpack 项目上从“babel-preset-es2015”转换为“babel-preset-env”时出现问题 - Issue when transitioning from "babel-preset-es2015" to "babel-preset-env" on React and Webpack project React Create App 中的 npm 构建和 babel 存在问题 - Problem with npm build and babel in React Create App 错误的ERR! 401 Unauthorized:@ babel / core @ 7.1.0使用创建新的反应应用程序时 - npm ERR! 401 Unauthorized: @babel/core@7.1.0 when using creating new react app npm 使用 create-react-app 时启动不运行 - npm start not running when using create-react-app 当在 react 中运行 npm test 时,Jest 给了我下面的消息,我要做的就是运行它默认在 App.test.js 中的测试 - When running npm test in react Jest is giving me the message below and All I am trying to do is run the test it comes by default inside App.test.js 运行'npm start'时从外部访问React App - Accessing React App externally, when running 'npm start' 运行 React App 时找不到模块“@babel/plugin-transform-react-jsx-source” - Cannot find module '@babel/plugin-transform-react-jsx-source' when running React App 如何提供 `babel-preset-react-app` 环境变量? - How to provide `babel-preset-react-app` env variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM