Open Clickys opened 6 years ago
By default parcel uses babel to transpile your code. If you use promised or async/await babel will polyfill it. But as you do not provide a polyfill get an error.
To fix this add a .babelrc
file to the root of your project. You will find the right configuration by googleing something like "babel no runtimeGenerator".
Thank you for answers,
By default if i want to use async/await which babel packages dependencies i should install for my project ?
Install babel-polyfill
and require it at the endpoint of your application, treeshaking should probably take care of trimming it down to as minimal of a polyfill as possible (once treeshaking is stable)
Thank you all for your help. One irellevant question before i close this post. When iam saving a module the web browser shouldnt automatically refresh ? It only refresh when i save the main file index.js
It should happen on every file, it might be a windows bug if you're on windows otherwise maybe safe-write?
@DeMoorJasper When i save a module in .js the old HTML is still render in the web browser, i have to do it manually is that normal ?
The HTML shouldn't change if you change js, it should reload through the socket that's been setup (called HMR). If it's just standard js (No framework) you'll probably have to write some HMR logic (either to refresh the page or handle HMR properly, probably easier to set it to refresh)
To babel7 users (Parcel >= 1.10.0):
{
"plugins": [
["@babel/plugin-transform-runtime", {
"corejs": 2
}]
]
}
This works for me. Just config .babelrc
as
{
"presets": [
["env", {
"targets": {
"browsers": ["last 2 Chrome versions"]
}
}]
]
}
following babel's docs solved my problem https://babeljs.io/docs/en/babel-plugin-transform-runtime
@Timespace7 how come it doesn't work with @babel/preset-env
?
I'm quite confused. I just tried parcel with Typescript and got the same error, but Typescript itself can already transform all code, why use Babel when?
The detection of when to supply a default browserlist
seems broken - it's triggered by this .babelrc
:
{ "presets":
[ [ "@babel/env"
, { "targets":
{ "firefox": 63
, "chrome": 71
, "node": 11
}
}
]
]
}
I have to add a redundant browsers
key or else things break! That's not great.
I'm quite confused. I just tried parcel with Typescript and got the same error, but Typescript itself can already transform all code, why use Babel when?
+++
This is really broken. Parcel doesn't look smart because of that :\
BTW, I've found a workaround for the issue by downgrading parcel to 1.9.7
and having a .babelrc
:
{
"plugins": [
["transform-runtime", {
"regenerator": true
}]
]
}
And adding babel-plugin-transform-runtime
(6.23.0) to my package.json
It works but meeeeeh :\
In Parcel 2, we'll apply polyfills automatically where needed using Babel 7's useBuiltIns: usage
option. See https://babeljs.io/docs/en/babel-preset-env#usebuiltins-usage-experimental.
This would be a breaking change for Parcel 1 to do this automatically, so we're waiting for Parcel 2.
@devongovett but wait, maybe it's needed for js but in case of typescript it sounds like a workaround for this problem. Isn't it's better to handle transpilation with tsc
(typescript compiler) when typescript is used? I don't need any babel for that case - it's unnecessary, wanna get rid of it.
Because, if it's using Babel, I will better use webpack. Actually, it's one of two things I worried about:
P.S. just two cents / two comments from me, ty
Hmm well the original issue wasnβt about typescript, but sure. Babel does currently run after typescript if needed. You can turn that off by setting up your browserslist properly.
That said, for parcel 2 weβre considering just compiling typescript with Babel instead of tsc since Babel 7 can do that now. Thatβs a separate issue though.
Hmm, yea, looks like there's no actual reason to use tsc
for compilation (except on pre-commit with something like husky):
https://iamturns.com/typescript-babel/
I suppose it's better to have some explanation in wiki, because on first sight it looks like it's a drawback of Parcel that it's using Babel compiler for typescript instead of TypeScript Compiler (tsc
)
Sorry, so how do I use async/await with a fresh parcel project? I'm targeting the latest browsers so I'd rather not include a polyfill if I don't need to.
@timwis You need to add a browserslist
to your package.json
file. Little demo here.
Works on Firefox, Safari, and Chrome on my machine; probably works for a few versions older as well, just no way of checking that here.
Otherwise, if you use TypeScript instead (you can just rename your files to have a .ts
extension, you don't need to actually use TypeScript), you don't need to add the browserslist
at all.
@Timespace7
This works for me. Just config .babelrc as
Can avoid adding .babelrc
by just adding this to your package.json
(babel automatically picks it up)
"browserslist": [
"last 2 Chrome versions"
]
EDIT: just noticed other people had mentioned browserslist too, but didn't see the actual code (for anyone looking for a quick solution)
Shouldn't at least parcel should understand what async
means and load the regeneratorRuntime?
Had the same behavior.
Got regeneratorRuntime is not defined
The most frustrating is that Chrome executed it when I opened that open w/o parcel
Is there any movement on this bug?
Facing this exact same issue now!
Quite frustrating, because parcel seems to do everything else smartly! Im going with promises as of now, as async/await seems to not work with parcel
For us, worked with:
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"@babel/plugin-proposal-class-properties": "^7.4.4",
"@babel/plugin-transform-runtime": "^7.4.4",
"@babel/preset-react": "^7.0.0"
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-runtime"
]
},
"browserslist": [
">0.25%",
"ie 11",
"not op_mini all"
]```
For us, it is not that it "works". The issue is we are targeting Chrome/Edge 74+ and we don't want or need the transform-runtime. So we got things working with this simple .babelrc, but it had to include the transform-runtime:
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"chrome": 74,
},
"exclude": ["transform-regenerator"]
},
"@babel/preset-react"
]
],
"plugins": [
["@babel/transform-runtime"]
]
}
Adding regenerator-runtime solved the problem for me.
// index.js
import 'regenerator-runtime/runtime'
@balazs4 As I said, if one is running Chromium V74+ there should be no need for the runtime. (Indeed I can use other bundlers with babel and that works without the runtime support).
See the second to last comment in https://github.com/parcel-bundler/parcel/issues/3012 Which talks about adding
"browserslist": ["last 1 Chrome versions"],
To the browserlist in package.json. That fixed this for me. keeps out the runtime.
Adding regenerator-runtime solved the problem for me.
// index.js import 'regenerator-runtime/runtime'
Thank you! Seems like there should be clear instructions on this. I tried three solutions before one worked.
Thanks @balazs4!
Adding regenerator-runtime solved the problem for me.
// index.js import 'regenerator-runtime/runtime'
This is what babel recommends!
@babel/polyfill
π¨ As of Babel 7.4.0, this package has been deprecated in favor of directly including core-js/stable (to polyfill ECMAScript features) and regenerator-runtime/runtime (needed to use transpiled generator functions):
import "core-js/stable"; import "regenerator-runtime/runtime";
Hello, I'm experiencing the same issue today and I wish that Parcel detects the need of whatever polyfill required by itself just like it does for everything else, and, by doing so, preserving 0-config comfort. Thanks !
install latest parcel v2
npm i -g parcel@2.0.0-alpha.3.2
fix this error
What about the programmatic API ? I use it on Parcel 1.x and I can't figure out how to use it on Parcel 2.x Thanks
@uncleGena thanks, it works
I still have this issue with parcel 2 alpha 3.2 and with a properly set browserslist
and only on build
. serve
works fine.
install latest parcel v2
npm i -g parcel@2.0.0-alpha.3.2
fix this error
That didn't solve the problem for me and parcel build
errors out with:
π¨ Build failed.
@parcel/bundler-default: The expression evaluated to a falsy value:
Whenever using .babelrc
with parcel@next on my typescript project I now get parsing error on serve and build. What's the actual workaround to this? I don't think importing transpile-focused libraries into my app's codebase makes too much sense as this should be more of a configuration option in the bundler.
With that said I now have an app that works nicely locally with parcel serve
(without .babelrc
) but doesn't run on production when using parcel build
due to the missing regeneratorRuntime
. And thinking this wasn't an issue at all with Typescript any more :(
I have managed to include regeneratorRuntime by simply setting the browserlist
property on my package.json
to something like "Chrome 70"
, as follows:
{
"name": "test",
"version": "0.0.1",
...,
"browserslist": "Chrome 70",
...
}
However, I don't understand why when NOT declaring browserslist
(which then defaults to >0.25%
), I still get the error even when using a modern browser with async/await support, i.e: Firefox 75 ?
However, I don't understand why when NOT declaring
browserslist
(which then defaults to>0.25%
), I still get the error even when using a modern browser with async/await support, i.e: Firefox 75 ?
Yup, same issue here. That makes no sense.
import 'regenerator-runtime/runtime'
This will work for Node because it creates a require('regenerator-runtime/runtime');
, so node will require the package.
This is not enough to make it work in the browser though, since require
doesn't exist there, in the browser we need to actually bundle regenerator-runtime/runtime
in our target file. To do this, I added this configuration to my package.json
:
"targets": {
"main": {
"context": "browser",
"includeNodeModules": true
}
}
(assuming your target is called "main")
This will bundle regenerator-runtime/runtime
(and any other external module that you import, you can also choose which ones by passing an array with their names instead of true
) into your target js file, instead of just trying to require
it.
This is only necessary if you want to support old browsers like IE11, if you don't, setting browserslist
to a more modern configuration should work, since the native async/await calls won't be transpiled.
@mischnic I am using the latest parcel 2.. what is the best way to solve this? I tried adding the import 'regenerator-runtime/runtime';
in my index.js file but the browser is still throwing the error
@devongovett @DeMoorJasper I am using the latest parcel 2 and still encountering this issue. I tried adding the import 'regenerator-runtime/runtime'; in my index.js file but the browser is still throwing the error. Also tried adding the browserlist but still to no avail. Help please!
@zavan are you using parcel 2? I am asking since I tried your solution but it didn't work. Maybe you can post your package.json for reference please? Thanks
@fab7jj after bumping into some other problems with Parcel I gave up on it and ended up using Webpack. I don't have the package.json anymore, sorry.
@fab7jj not sure I can be much help without a reproducible repository of some sort, importing that package should solve it...
@DeMoorJasper to be honest with you all I am doing is using the async/await syntax and this issue popped up in the browser when that part of the code gets executed only when using the 'build' command, since while using the 'watch' or 'serve' command it is fine which is strange. Do i need to do something else besides importing that package in my index.js? declare it in the packagae.json or something? In parcel 2 there is no support for babel.rc file right?
babel.rc
does not exist but .babelrc
is definitely supported, we even support babel.config.json
now and use the official config resolver by Babel. So it's actually way better than Parcel 1 in that perspective... We pretty much have the same amount or even more features than Parcel 1 currently has.
Like I said it should work, I've never had this issue before and use async all the time
@DeMoorJasper Can you point me to some documentation on how to wire in this .babelrc config file in parcel 2 since I have seen a solution that requires installing the plugin: @babel/plugin-transform-runtime and then declare it in such file but I can't try it out since a rookie like me have no clue where to put it.
That is strange then how some of us are having this issue, the only way I now made it work is by adding the below to the package.json
"browserslist": [ "Chrome 78" ],
Only Chrome 78 worked, for example: last 1 Chrome versions did not work, but i fear that this is more of workaround rather than a proper solution
Thanks a lot
+1 for @zavan, I mean, I can understand that devs wouldn't like to fix issues that happens in v1.x while actively developing v2.x, but at least v2.x should have fixed the issue, but it didn't and that's a shame. And I'm not even talking about v2.x being very much less zero-config than v1.x...
π bug report
Iam trying to import and export a class from a module and i get this behavior. Every time iam trying to import the class or export it i get regeneratorRuntime .
π Configuration (.babelrc, package.json, cli command)
π€ Expected Behavior
Just import/export the class to the index.js
π― Current Behavior
Instead of import/export the class i get the follow error :
js\models\foodSearch.js:6 Uncaught ReferenceError: regeneratorRuntime is not defined at js\models\foodSearch.js:6 at js\models\foodSearch.js:12 at Object.parcelRequire.js\models\foodSearch.js.axios (js\models\foodSearch.js:12) at newRequire (js.ee4a09a9.js:48) at localRequire (js.ee4a09a9.js:54) at Object.parcelRequire.js\index.js../models/famousChefQuotes (js\index.js:3) at newRequire (js.ee4a09a9.js:48) at parcelRequire.js\models\famousChefQuotes.js (js.ee4a09a9.js:80) at js.ee4a09a9.js:106
........\AppData\Roaming\npm\node_modules\parcel-bundler\node_modules\util-deprecate\browser.js:14 Uncaught ReferenceError: regeneratorRuntime is not defined at ........\AppData\Roaming\npm\node_modules\parcel-bundler\node_modules\util-deprecate\browser.js:14 at ........\AppData\Roaming\npm\node_modules\parcel-bundler\node_modules\util-deprecate\browser.js:41 at Object.parcelRequire.js\models\SearchFood.js.axios (........\AppData\Roaming\npm\node_modules\parcel-bundler\node_modules\util-deprecate\browser.js:45) at newRequire (js.ee4a09a9.js:48) at localRequire (js.ee4a09a9.js:54) at Object.parcelRequire.js\index.js../models/famousChefQuotes (js\index.js:3) at newRequire (js.ee4a09a9.js:48) at parcelRequire.js\models\famousChefQuotes.js (js.ee4a09a9.js:80) at js.ee4a09a9.js:106
π» Code Sample
https://gist.github.com/Clickys/dd0d2ef946b15b0bf34b1ac23ecab3e0
π Your Environment
+-- animate.css@3.6.1 +-- axios@0.18.0 +-- eslint@4.19.1 +-- eslint-config-airbnb-base@13.0.0 `-- eslint-plugin-import@2.13.0 parcel latest: '1.9.7'