-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES Module type=module cause "parcelRequire is not defined" error #1401
Comments
since you use type="module",why use parcel? |
Mainly for module resolution. |
A good point to use Parcel in this case is the minification and files with hashes |
I have the same problem in different context. I'm trying to consume parcel-bundled module from my webpack-bundled app. Since ES6 modules (my app) run in strict context an attempt to consume the module results in assignment to undefined variable and crashes my app. |
Edit: disregard this, still reproducible. |
Maybe it makes sense for Parcel to remove |
This is an issue with the way Here's what Parcel's output looks like: parcelRequire = (function (modules, cache, entry, globalName) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof parcelRequire === 'function' && parcelRequire;
var nodeRequire = typeof require === 'function' && require;
function newRequire(name, jumped) { Notice how the global parcelRequire is implicitly declared without let/var! By editing the output file and naively adding An alternative that works is changing it to I ran into this in the wild in a slightly separate case when attempting to require a library I bundled with Parcel in a Webpack project. The Webpack project had a top level |
Looks like a duplicate of #864 |
Adding the Firefox error message so this issue pops up in searches:
Can this issue be resolved without altering the generated output (e.g. assigning to |
it should not be hard to add |
(duplicate of #864 (comment)) |
temporary fix for your work environment or localhost - https://twitter.com/dfkaye/status/1044693110700171264 |
nicer work around: .assetWarpper.js const path = require('path')
const CWD = process.cwd()
const PACKAGE = require(path.join(CWD, 'package.json'))
const varRequire = async ({name, bundler}) => {
// name = app.ere76r5e76r5e76r.js
if (name.split('.').pop() === 'js' && bundler.options.production) {
return {
header: `var parcelRequire = undefined;`,
footer: ``
}
}
}
module.exports = varRequire; |
We're having same issue! |
Note that simply removing |
Sooo, am I reading this correctly? Parcel does not allow for a standard ES Module output? |
yeah I'm having this issue when I use a raw .ts file as my input and I bundle my node_modules with it into a single file e.g.
when i run it in node I'll get an error like
when I look at the non-minified version I can see
so my guess is the assumption was that it would globally assign. it appears that it doesn't. Obviously if I add I came up with a workaround
obviously the bummer here is I can't use anyway is there an ETA for this fix? the set-up (or lack thereof) is super convenient so I'd like to use Parcel for a recommended way of doing something except this is kind of a blocker. |
We ran into another issue specific to our app, so we had to come up with a post-bundling process file that uses the parcel-bundler API directly (see https://parceljs.org/api.html) in order to solve that as well as the "parcelRequire is not defined" issue. https://gist.github.com/dfkaye/f43dbdfbcdee427dbe4339870ed979d0 |
Have the same issue here! "ReferenceError: parcelRequire is not defined" While working in: Command: Can fix if manually add let,const before parcelRequire= in the dist/output file |
A workaround for Gulp / stream-based build system users, using gulp-insert to prepend the const gInsert = require('gulp-insert')
let parcelPatchTask = ()=>{
return gulp.src(p(paths.dist, '**/*.js'))
.pipe(gInsert.prepend(';var parcelRequire;\n'))
.pipe(gulp.dest(paths.dist))
} |
…dress ReferenceError (issue parcel-bundler#1401)
…ReferenceError (issue parcel-bundler#1401)
Please see my PR above. It's pretty disappointing that there was no fix or PR for this show-stopper bug that requires literally a 4-character fix, for a 30k star project used by so many people :/ |
My use is to use native ES modules for dev and then bundler for production for older browsers. I also happen to be using snowpack. If you say Parcel will not work in this case without rewriting the script include structure between dev and deploy it cannot be used. :( While I get the error everyhting seems to work. So does the error matter? |
if you strip <script type="module"> you need to add |
I'm not using |
Observation: In addition to that LOL fix from Twitter, this issue can also be hackishly fixed by inserting any regular script tag ( not type=module ) before your type=module entry point. Without it Parcel still generates these errors. <script src="seemingly_anything.js"></script>
<script src="es6_entry_point.js" type="module"></script> |
I get this error whether I use EDIT: more testing |
For me, this error also happens when I put a Edit: Actually, the console says that the connection was lost. But on file changes, the page still appropriately updates... |
When I remove type="module", github pages does not show any styles on my page |
One does not simply delete type="module" from the code. Especially if you want to use native ES modules. I have strong believe that defer and async attributes do not give us that — they do not provide module isolation, that's why it's impossible to use imports there. When you delete type="module", you loose power of native ES modules. I think that it's good if some web app can work in modern browsers "as is", without a preprocessing. When I use ES modules, modern browsers understand how to process them. The problem is: when I add the parcel bundler, all goes wrong. |
I used this workaround. |
Thank you so much for pointing this out by @dfkaye from this thread. Much better and more elegant fix, without losing the power of type module! If only this was magically fixed in V2.. |
Will Parcel ever support ES6? Issue open since 2018, check clock, it's three years by now |
@kungfooman parcel v2 supports type=module |
@talentlessguy Did you try that yourself? I just installed
But it still produces same error as in this issue |
@kungfooman there's an example in this thread on how to do this #1401 (comment) |
Parcel has some [misunderstandings between script tags declared as es module](parcel-bundler/parcel#1401) and it's cascading into the build as console errors inkandswitch#22 inkandswitch#21 Removing the attribute sorts this out. until they fix it in Parcel.
Add a |
@doggy8088 which version are you using this fix for? |
@mukaofssn |
🐛 bug report
Using ES Module in main HTML cause "parcelRequire is not defined" error.
If we use type="module", minimal code in Getting Started (Parcel official) make this error.
🎛 Configuration (.babelrc, package.json, cli command)
zero configuration.
🤔 Expected Behavior
If we write super simple code with ES Module like below,
I hope it is bundled properly and get "hello parcel" in console without error.
😯 Current Behavior
In console,
💁 Possible Solution
Apparently ES Module cause this error.
When I removed type="module" from script element, no error observed.
It suggest that we can avoid error by avoiding type="module" for parcel.
But I think this approach is not good.
ES Module (type="module") is generally available (now 2018-05, all popular browsers supported!!), so ES module is straight forward way to import module.
🔦 Context
I do not know what problem is caused by this error.
🌍 Your Environment
Thank you good library, I hope this project become better and better.
The text was updated successfully, but these errors were encountered: