简体   繁体   English

未捕获的 ReferenceError:RegeneratorRuntime 未在 React 中定义

[英]Uncaught ReferenceError: regeneratorRuntime is not defined in React

I'm getting an error "Uncaught ReferenceError: regeneratorRuntime is not defined".我收到错误“未捕获的 ReferenceError:未定义 regeneratorRuntime”。 Please help me to find out the error and how to resolve it.请帮助我找出错误以及如何解决它。

在此处输入图像描述

  • Install the runtime dependency安装运行时依赖
npm i --save-dev @babel/plugin-transform-runtime
  • Add the plugin to your.babelrc file将插件添加到 your.babelrc 文件
{
  "plugins": ["@babel/plugin-transform-runtime"]
}

More Info: https://babeljs.io/docs/en/babel-plugin-transform-runtime更多信息: https://babeljs.io/docs/en/babel-plugin-transform-runtime

TLDR; TLDR;

  • Async functions are abstraction on top of generators.异步函数是生成器之上的抽象。
  • Async functions and generators are now supported in all major browsers and in Node10 and upwards.现在所有主要浏览器和 Node10 及更高版本都支持异步函数和生成器。
  • If you are using a transpiler (such as babel) for backwards compatibility, you would need an extra "layer" that transforms generators.如果您使用转译器(例如 babel)来实现向后兼容性,则需要一个额外的“层”来转换生成器。 This implies transforming ES6 into ES5 at runtime since their syntax isn't backwards compatible.这意味着在运行时将 ES6 转换为 ES5,因为它们的语法不向后兼容。 See https://cmichel.io/how-are-generators-transpiled-to-es5https://cmichel.io/how-are-generators-transpiled-to-es5

Thanks It works when I add an import statement -- import regeneratorRuntime from "regenerator-runtime";谢谢 当我添加一个导入语句时它起作用了—— import regeneratorRuntime from "regenerator-runtime"; in the component i am using async/await.在我使用异步/等待的组件中。

Ran into this problem (using Babel v7) and even after following the advice and installing relevant packages, I was still unable to get id of this error.遇到了这个问题(使用 Babel v7),即使按照建议安装了相关软件包,我仍然无法获得这个错误的 id。 following stack overflow posts were checked...检查了以下堆栈溢出帖子...

Following actions helped:以下行动有所帮助:

  1. Go to package.json & add the following inside 'jest' (screenshot added also): Go 到 package.json 并在“笑话”中添加以下内容(还添加了屏幕截图):

"moduleNameMapper": { “模块名称映射器”:{
".+\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy" } ".+\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "identity-obj-proxy" }

在此处输入图像描述

  1. when running a test use the following suffix in the command...运行测试时在命令中使用以下后缀...

--setupFilesAfterEnv "./src/setupTests.js" --setupFilesAfterEnv "./src/setupTests.js"

so to run a test, it will be:所以要运行测试,它将是:

$ jest /pathToTest/TestFile.test.js --setupFilesAfterEnv "./src/setupTests.js" $ jest /pathToTest/TestFile.test.js --setupFilesAfterEnv "./src/setupTests.js"

Hope it helps someone like it helped me...希望它可以帮助像它帮助我的人...

If it's really necessary for you to use the async function then the solutions above should work.如果您确实需要使用异步 function,那么上述解决方案应该可以工作。 Another way to resolve this is to use regular promises, at least that was in my case.解决此问题的另一种方法是使用常规承诺,至少在我的情况下是这样。

just add只需添加

  "browserslist": [
    "last 2 Chrome versions"
  ]

at the end of your projects package.json file, also see that its plural browsers not browser!在您的项目package.json文件的末尾,还看到它的复数浏览器不是浏览器!

Your file in the end might look something like this ->你的文件最终可能看起来像这样 ->

  },
  "dependencies": {
    "prop-types": "^15.8.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  },
  "browserslist": [
    "last 2 Chrome versions"
  ]
}

ignore the dependency section in the above code view, its just for reference on how your package.json might look.忽略上述代码视图中的依赖部分,它仅用于参考 package.json 的外观。

2022 2022

If you're working with Babel 7 or later version, you don't need to install an extra plugin (neither @babel/plugin-transform-runtime or @babel/plugin-transform-regenerator or other plugins ).如果您使用的是 Babel 7 或更高版本,则不需要安装额外的插件( @babel/plugin-transform-runtime@babel/plugin-transform-regenerator其他插件)。

Later, you have to include this statement every time you're using async/await syntax.之后,每次使用async/await语法时都必须包含此语句。

import regeneratorRuntime from "regenerator-runtime";

Maybe if you have set a linter in your project it will warning you about that statement is declared but its value is never read, but I think is just an error, because if you delete it the code doesn't work.也许如果您在项目中设置了一个 linter,它会警告您该语句已声明但它的值从未被读取,但我认为这只是一个错误,因为如果您删除它,代码将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的 ReferenceError:未定义 regeneratorRuntime - Uncaught ReferenceError: regeneratorRuntime is not defined 反应:ReferenceError:regeneratorRuntime 未定义 - React: ReferenceError: regeneratorRuntime is not defined Babel 7 - 未捕获的 ReferenceError:未定义 regeneratorRuntime - Babel 7 - Uncaught ReferenceError: regeneratorRuntime is not defined 反应异步并等待引发错误“未捕获的ReferenceError:未定义regeneratorRuntime” - React async and await throwing error 'Uncaught ReferenceError: regeneratorRuntime is not defined' 未捕获的 ReferenceError:regeneratorRuntime 未在反应 17、webpack 5 中定义,同时通过操作进行 api 调用 - Uncaught ReferenceError: regeneratorRuntime is not defined in react 17, webpack 5 while making api calls through actions 未捕获的 ReferenceError:React 未定义 - Uncaught ReferenceError: React is not defined 未捕获的 ReferenceError:React 未定义 - Uncaught ReferenceError: React is not defined 未捕获ReferenceError:未定义$(反应)? - Uncaught ReferenceError: $ is not defined (react)? 无服务器 create-react-app - ReferenceError: regeneratorRuntime 未定义 - serverless create-react-app - ReferenceError: regeneratorRuntime is not defined Rete 库给 React JS 带来错误:ReferenceError: regeneratorRuntime is not defined - Rete Library giving error with React JS : ReferenceError: regeneratorRuntime is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM