[英]Uncaught ReferenceError: regeneratorRuntime is not defined
[英]Babel 7 - Uncaught ReferenceError: regeneratorRuntime is not defined
我收到錯誤Uncaught ReferenceError: regeneratorRuntime is not defined
using React with webpack and Babel 。
我已經按照這個答案通過定義我的.babel.rc
為:
{
"presets": ["@babel/preset-env", "@babel/preset-react"] ,
"plugins": [
["@babel/plugin-transform-runtime"]
]
}
並運行:
npm i --save-dev @babel/plugin-transform-runtime
但是,之后我得到了完全相同的錯誤。 我也跟着this other answer和this one ,但仍然得到完全相同的錯誤。
我在package.json
中的 babel 特定安裝如下:
"dependencies": {
"@babel/runtime": "^7.14.6"
},
"devDependencies": {
"@babel/core": "^7.14.6",
"@babel/plugin-transform-runtime": "^7.14.5",
"@babel/preset-env": "^7.14.7",
"@babel/preset-react": "^7.14.5"
}
有任何想法嗎?
這最終對我有用:
我的問題是我在.babel.rc
文件和webpack.config.js
文件中都定義了 babel 插件。 我需要從我的webpack.config.js
刪除該插件,並僅在我的.babel.rc
文件中使用它。 然后它運作良好。
{ "預設": [ [ "@babel/preset-env", { "targets": { "node": "9.2.1" } } ], "@babel/preset-react" ] }
這是我的文件.babelrc
看:
@babel/preset-env 是一個智能預設,它允許您使用最新的 JavaScript,而無需對目標環境需要哪些語法轉換(以及可選的瀏覽器polyfills )進行微觀管理。 這既使您的生活更輕松,而且 JavaScript 包更小!
你的問題:
您正在使用“@ babel / preset-env”,您必須指定要編譯的節點版本。 “節點> 7.6”。 我推薦10。
為什么node > 7.6 Node.js 7.6 提供了對默認啟用的 async/await 的官方支持,並在低內存設備上提供了更好的性能。
如何指定版本:很簡單
target.node 字符串 | “當前的”。
If you want to compile against the current node version, you can specify "node": "current", which would be the same as "node": process.versions.node.
對我來說是這樣的:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "9.2.1"
}
}
],
"@babel/preset-react"
]
}
這可以讓編譯器理解 ASYNC AWAIT,希望對你有幫助!
嘿,我遇到了同樣的問題,我使用的是 Babel 7,我安裝了這兩個依賴項:
npm install --save @babel/runtime
npm install --save-dev @babel/plugin-transform-runtime
並且,在 .babelrc 中,添加:
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/transform-runtime"]
]
}
這解決了我的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.