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
4.0.189: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides) #17245
Comments
Export
|
Is this a solution @jiadesen ? I'm getting them same as you as jut trying out v4 in a new project and getting:
See https://elixirforum.com/t/importing-pdf-mjs-into-app-js/59578 with --target=es2022 that is via esbuild. If I use the default of --target=es2017 I get:
Should work or should we go back to v3? |
No, it's just how you're importing it. Have switch to that way to confirm that we have the same issue. We do. |
Searching for the error message Looking at the MDN compatibility data all modern browsers/environments support top level await, and has done so for awhile, hence the only suggestion that we can really provide is to (if possible) use another bundler instead. |
Solved by adding vite configuration:
|
I also encountered the same error, but I was able to resolve the issue by installing the following vite plugin. |
this should be the final answer
|
this fixed it for me, thank you! |
I have the same issue here. I am trying to use pdf.js v4 in an elixir/phoenix framework environment where the bundler is esbuild. Should it work or do we have to switch back to v3? |
It works better, compatible with |
For anyone wanting broader browser support - https://www.npmjs.com/package/vite-plugin-top-level-await. {
// vite.config
plugins: [
// ...
topLevelAwait({
promiseExportName: '__tla',
promiseImportName: i => `__tla_${i}`,
}),
],
} Note: Bundle size will be 30kb larger compared to native es2022. |
And if i'm working in Angular project that's been created with Angular CLI (NO VITE CONFIGURATION FILE !), what should i do ? How to solve this issue with projects that does not based on vite ? |
Agree with @farisshomali, angular out-of-the-box uses Webpack, there is possibility to customize its configuration with (experiments: { topLevelAwait: true }), but it means stick to webpack (not good). Trying to use 'esbuild' (planned to be used by Angular out-of-the-box) and pdfjs - build fails ('esbuild' has no intention to support toplevelawait' at all). So, question is can 'top level await' be avoided by 'pdfjs'? It will make it easier to use any bundler. |
@Snuffleupagus ↑ ? |
@yuri-apanasik I don't know how you use the pdf.js project, but I think it's possible to avoid use "top level await". The top level await is used to load the main bundle of pdf.js, it contains all core features any viewer solution will depends on. The pdf.js also has a web bundle, it contains all code the default viewer needs, and of course it depends the main bundle. The possible way to avoid the top level await is develop a custom gulp task, just build a single bundle which combine the web and main bundles together, then the issue will gone. it's may not be an easy job depending on your knowledge of the pdf.js project. |
@Priestch yes, I am new in pdf.js, we just looked for the tool to convert PDF to PNG on the client side and found pdf.js. So, as lazy developers we just took pre-built 'pdfjs-dist' package and faced with bundler issue. For now we are on PoC phase, so as a solution we just downgraded 'pdfjs-dist' to V3. For the production looks like we should go with custom build solution. Thank you for the suggestion! |
@yuri-apanasik What do you mean "on the client side"? You used pdf.js in browser or node environment? If you use it in browser, all you need is comment out two lines code, and run gulp task You can also change the two files to flatten and replace the Line 305 in 833d7ac
// line 305, 306
// libraryAlias["display-node_stream"] = "src/display/node_stream.js";
// libraryAlias["display-node_utils"] = "src/display/node_utils.js"; |
@Priestch I mean in browser, yes. Thank you one more time for the hint! We'll try to make our own pdfjs build. But as you can see it is not only my problem, a lot of developers who want to easily use pre-built package face with this problem. One of the minimal ways to reproduce is:
Result -> build failed. As you suggested situation can be fixed with custom build, but it makes 'pdfjs' minimal usage example more complicated. |
@Priestch If only in a browser environment, I'd like to know what other code could be annotated and whether this could significantly reduce the size of the build artifact |
@jiadesen it's just quick hack to fix the issue, I think it doesn't reduce the build artifact. |
This seems like a bug in angular, if a default angular app crashes when using a stable JavaScript feature. It's like saying "please don't use classes because Angular has a bug and doesn't support them". |
I don't agree this is an issue with Angular. The v3 works for us for now but we had some badly rendered pdfs lately and want an upgrade. The only v3 that worked so far was 3.8.162 due to svg typings issues. We only use basic rendering so never dug deep enough to understand this library. Both builders in angular produce some errors, the common one being this top-level-await. While I understand Angulars position (zone.js etc), I don't know pdf.js enough to understand forcing incompatible builds. Moreover - there is this legacy build available yet somehow it can't be imported - when building the app it throws not found errors. We are running out of ideas how to workaround the workaround. |
@MikeDabrowski if you are using default Angular configuration with Webpack, you can customize it by installing '@angular-builders/custom-webpack' NPM package and changing '@angular-devkit/build-angular' to '@angular-builders/custom-webpack' in angular.json (some more adjustments should be done, just google it). When you can create the following custom webpack config to avoid top-level-await error: At the same time @Priestch already suggested the solution with custom 'pdfjs' build. @nicolo-ribaudo yes, it is NOT angular bug. It is about bundlers, 'esbuild' for example have no intention to support top-level-await at all (evanw/esbuild#253). So, 'pdfjs' in its full version (without commenting out some unnecessary code in case of usage in browser) can't be used with 'esbuild'. 'webpack'/'vite' need custom configuration. So, trying to make an improvement in V4 with es modules (if I got it right) it appeared that developers who used V3 easily now have to spent hours to understand what is wrong with their build using V4... |
@yuri-apanasik - yep, im just doing it - but v4 changes the location of workers and my app needed them I think. So trying to somehow work that out.
I wouldnt mind that custom build, we could host it. But I dont want to waste time reading how to do it. As you said in one of your comments its like in the 90s -here is kernel, now code build yourself an OS to write play asteroids or sth... Aint nobody got time for that. Any chance someone posts how to prep whole build? For reference our usage is for example: - I am not even sure if we need the workers
|
@Priestch Just for information... I've tried to make custom pdfjs build, there were some troubles: for example, 'node-canvas' have no arm64 build, so had to solve this issue for M1. Finally pdfjs was ok, but unfortunately when I copied result files into the project and build (actually build was also successful) I faced with error in runtime (some class used before its declaration). |
Can we get this issue reopened ? This has not been resolved yet. @yuri-apanasik That is exactly what I was afraid of - more issues when making custom build. The reason we wanted to update to v4 is that we had one incident with pdf badly rendered - some border was misplaced. Could not reproduce it unfortunately, so now we are waiting and keeping an eye for such errors. |
@yuri-apanasik If you only use in browser environment, you can safely delete |
@Priestch can you please show this change?
|
…evel await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides)` error [link](mozilla/pdf.js#17245)
I'm also struggling with this issue. I upgraded my Angular project to pdf.js v4, struggled with Angular's build system, got it working. Now I'm upgrading Angular from 17.0 to 17.2 and Angular is giving me warnings about using topLevelAwait again. Angular says they do not support top-level await. That means that developers have to either a) arm-wrestle the Angular compiler into something it's not meant for, b) hack together a custom build of pdf.js as @yuri-apanasik has attempted to do, or c) stay on an old version of pdf.js until that eventually stops working. I keep solving this issue and then having to re-solve it every time I upgrade a piece of my project. Top-level await has been described in this thread as a "stable JavaScript feature", and that may be true according to the specification, but compiler support for that feature is not yet widespread enough to provide a stable experience. Angular provides two new build systems with v17; neither one supports this feature. |
I totally agree with @JHarrisGTI. pdf.js would totally also work without top-level await. Please provide us a version of it which is compatible with the compilers of all big frameworks, including Angular! |
Note: The reason that we went with top level await in the re-factoring for PDF.js version It should be possible to remove top level await from the PDF.js builds, and I've even got WIP patches locally. If you'd like to see top level await be removed from the builds and are serious about paying for that be implemented, please contact me privately. (Email address can be found in my GitHub profile.) Please keep in mind that the primary development target of this library is the Firefox PDF Viewer, and the fact that the general PDF.js library can still be used elsewhere is thanks to a lot of time/effort spent by a few unpaid contributors (such as myself) over the years.
|
@Snuffleupagus as I can see you mentioned my comment, but believe me I had no intention at all to offend you somehow. This is just a visualization how the situation looks like for the person who doesn't know at all the story behind and just is trying to use 'pdfjs' as any other package. I am sure number of downloads and lots of projects where 'pdfjs' is used gives you much more understanding how valuable your work is, than any words. But it will be clear truth (as everything written above) if I will add "Thank you" for you and for all developers who was involved into implementation of this great library that solves for all of as such a complicated thing as working with pdf in browser. Nice to hear there is a chance it will be compatible with 'esbuild', hope complexity growth is not critical. |
@Snuffleupagus I'm definitely interested in having that fixed as a lead maintainer of React-PDF. Please kindly submit $50 expense on https://opencollective.com/react-pdf-wojtekmaj :) |
@Snuffleupagus My company is also interested. I emailed you a few days ago but I'm not sure if it went through. Hopefully it didn't get stuck in a spam folder. |
@JHarrisGTI Thanks, I've replied to the email so let's continue there :-) |
in reactjs using pdfjs-dist (npm) v4.2.67 and vite: install:
update vite.config.ts:
import:
my use:
im my case i only need a thumbnail of pdf... |
@JHarrisGTI The PR landed earlier today, and I've tried to reach you (and your colleague) by email; did it get stuck in a spam filter perhaps? |
@Snuffleupagus I got your email about the PR yesterday; I believe Adam has emailed you about payment details. I also reached out to our IT department and they say you shouldn't get stuck in the spam filter again. 🙂 |
Attach (recommended) or Link to PDF file here: any
Configuration:
The text was updated successfully, but these errors were encountered: