babel: ReferenceError regeneratorRuntime is not defined
I’m building an SSR template, and when I use @babebl/register and then execute webpack (config), the system reported an error. I tried @babel/polyfill and @babel/plugin-transform-runtime, but none of them worked.
Input code
index.js:
require("ignore-styles");
require("@babel/register")({
ignore: [/\/(build|node_modules)\//],
presets: ["@babel/preset-env", "@babel/preset-react"],
plugins: ["@babel/plugin-syntax-dynamic-import", "dynamic-import-node"]
});
require("./server");
server.js
const config = require('../config/webpack.dev.config.js')
const webpack = require('webpack')
const express = require('express')
const webpackDevMiddleware = require('webpack-dev-middleware')
const reactRouter = require('react-router')
const StaticRouter = reactRouter.reactRouter;
const app = express()
const complier = webpack(config)
const PORT = 8090
...
app.use(webpackDevMiddleware(complier, {
publicPath: config.output.publicPath
}))
app.listen(PORT , function() {
console.log(`SSR running on port ${PORT }`);
})
webpack.dev.config.js
const paths = require('./paths')
const webpack = require('webpack')
const htmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: ['webpack-hot-middleware/client?reload=true', paths.appIndexJs],
output: {
path: paths.clientBuild,
filename: '[name].bundle.js',
chunkFilename: '[name].bundle.js',
publicPath: paths.publicPath
},
devtool: 'eval-source-map',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env', {
useBuiltIns: 'usage',
corejs: "2",
targets: {
browsers: ['last 2 versions', 'ie >= 9'],
},
}],
'@babel/preset-react'
]
}
}
]
},
resolve: {
extensions: ['.js', '.jsx']
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new htmlWebpackPlugin({
template: './client/index.html',
filename: 'index.html'
})
]
}
Error message
Environment
- Babel version(s): 7.4.3
- Node/npm version: [e.g. Node 10.15.3/npm 6.4.1]
- OS: window 10
Rep
https://github.com/xuchenchenBoy/ssr (Please execute npm run dev:server and release notes in server.js)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 22
- Comments: 68 (5 by maintainers)
Links to this issue
Commits related to this issue
- resolving asynchronos event caused after completing adding the google places API. This was a general babel issue as explained here https://github.com/babel/babel/issues/9849 — committed to lanre-mark/vote-assister by lanre-mark 4 years ago
- require regenerator-runtime This fixes the following error, introduced in #7. ReferenceError: regeneratorRuntime is not defined https://github.com/babel/babel/issues/9849 — committed to mortend/transpiler by mortend 4 years ago
- Resolved regeneratorRuntime issue See [this issue](https://github.com/babel/babel/issues/9849#issuecomment-592668815) — committed to furkanacaryes/jest-preset-preact-es by deleted user 3 years ago
- DX-1516: Add Babel config and require Regenerator runtime Add `babel.config.js` and require `regenerator-runtime` to fix these problems: https://github.com/babel/babel/issues/12018 https://github.co... — committed to SwedbankPay/search.developer.swedbankpay.com by asbjornu 3 years ago
- Chore: modify babel configuration due to reference error - https://github.com/babel/babel/issues/9849 — committed to to-the-space/to-the-space by seojuny95 3 years ago
- chore: makes sure that compiled code is compatible with nextjs - https://web.dev/serve-modern-code-to-modern-browsers/ - https://github.com/babel/babel/issues/9849 — committed to namjul/gspenst by deleted user 3 years ago
- (chore): resolved ReferenceError regeneratorRuntime is not defined (https://github.com/babel/babel/issues/9849) — committed to adenekan41/scoutbar by adenekan41 3 years ago
I had this issue using rollup with babel. I just used this babel config to resolve it :
You have used at least one of these features and it’s not supported in at least one of your
target
s (['last 2 versions', 'ie >= 9']
):async
;function*
); and/orasync
generatorsAs a result,
@babel/preset-env
decides to use@babel/plugin-transform-regenerator
, which relies onregeneratorRuntime
being available globally.To fix this, simply use
regenerator-runtime
, following the instructions in its README to ensureregeneratorRuntime
is made available globally.Thanks. This worked for me:
npm install --save-dev @babel/plugin-transform-runtime
It feels like https://babeljs.io/docs/en/babel-preset-env could be improved, because currently it is unclear how to properly set up regenerator. Examples on different ways of doing would be great.
this solution works for me:
This thread is a sad testament into how blown to shit the situation is.
🙏🙏🙏 Here’s a few prayers for future lost souls 🙏🙏🙏
Easiest way to fix this ‘regeneratorRuntime not defined issue’ in your console:
You don’t have to install any unnecessary plugins. Just add:
<script src="https://unpkg.com/regenerator-runtime@0.13.1/runtime.js"></script>
inside of the body in your index.html. Now regeneratorRuntime should be defined once you run babel and now your async/await functions should be compiled successfully into ES2015
@PenguinTamer Did you try using
useBuiltIns: usage
,@babel/plugin-transform-runtime
or importingregenerator-runtime
?I can’t get it to work, and Webpack doesn’t seem to allow this snippet in any kind of way. I’m guessing most people use Webpack (and its config file isn’t even preset if create-react-app is used) so what is the solution for the rest?
I installed
@babel/plugin-transform-runtime
within the app as well asregenerator-runtime
globally, by the way.Note that
@babel/polyfill
has been deprecated for a while. To solve theregeneratorRuntime
problem, you can do this:npm install --save regenerator-runtime
entry: ["regenerator-runtime/runtime.js", "<your enter js file>"]
Babel 7 Users
I had some trouble getting around this since most information was for prior babel versions. For Babel 7, install these two dependencies:
And, in .babelrc, add:
I’m use
@babel/plugin-transform-runtime
, still got this issue.Install babel-polyfill
npm install --save @babel/polyfill
Update webpack file
entry: ["@babel/polyfill", "<your enter js file>"]
import 'regenerator-runtime/runtime'
This fixed my issue too. Thanks!
This worked for me thanks @sylvainDNS
i have to write it like this though
in my
.bablerc
No other plugins are needed.
“useBuiltIns”: “entry”:
then start your main.js with this:
regenerator-runtime/runtime
is @babel/preset-env dependency.“useBuiltIns”: “usage”
No extra imports are required. All pollyfills will be loaded automatically based on your code and Webpack will do the rest.
As for the targets I think it’s best to rely on .browserslistrc or browserslist in package.json instead of overriding it in .babelrc:
for me
CRA already includes
@babel/plugin-transform-runtime
: https://github.com/facebook/create-react-app/blob/3f699fd08044de9ab0ce1991a66b376d3e1956a8/packages/babel-preset-react-app/create.js#L162I did and resolved my issue, thank you for your answer though!
Sent from my iPhone
Where do you put this?
import 'regenerator-runtime/runtime'
in the root file andimport 'regenerator-runtime/runtime'
in my jest.config.js helpedWe’re using Babel 6 (for reasons) and this worked for us, thanks very much!!!
Still not working for me.
Update: After updating to Babel 7, I found this page helpful. https://stackoverflow.com/questions/53558916/babel-7-referenceerror-regeneratorruntime-is-not-defined
I had a similar problem but I don’t use
.babelrc
. I solved by writing that inwebpack.config.js
:this one did it for me
Wrong place for babel presets! Check the docs for babel configuration or webpack’s babel-loader examples.
None of these are working for me. What one worked for others?
Bear in mind that adding
esmodules: true
will lead tobrowsers
target being ignored if you included it too, as per Babel docsThankssss, Mate!! Like a charm