Closed
Description
ERROR Failed to compile with 2 errors 12:58:23
error in ./node_modules/pdfjs-dist/build/pdf.js
Module parse failed: Unexpected token (2413:45)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| intent: renderingIntent,
| renderInteractiveForms: renderInteractiveForms === true,
> annotationStorage: annotationStorage?.getAll() || null
| });
| }
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/sign/esign/AcEsign.vue?vue&type=script&lang=js& 122:0-41 493:6-17
@ ./src/views/sign/esign/AcEsign.vue?vue&type=script&lang=js&
@ ./src/views/sign/esign/AcEsign.vue
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/sign/Home.vue?vue&type=script&lang=js&
@ ./src/views/sign/Home.vue?vue&type=script&lang=js&
@ ./src/views/sign/Home.vue
@ ./src/router/index.js
@ ./src/js/core/init.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://192.168.43.252:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
error in ./node_modules/pdfjs-dist/web/pdf_viewer.js
Module parse failed: Unexpected token (895:20)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| this._on(eventName, listener, {
| external: true,
> once: options?.once
| });
| }
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/sign/esign/AcEsignViewer.vue?vue&type=script&lang=js& 23:0-64 97:28-36 110:30-39
@ ./src/views/sign/esign/AcEsignViewer.vue?vue&type=script&lang=js&
@ ./src/views/sign/esign/AcEsignViewer.vue
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/sign/esign/AcEsign.vue?vue&type=script&lang=js&
@ ./src/views/sign/esign/AcEsign.vue?vue&type=script&lang=js&
@ ./src/views/sign/esign/AcEsign.vue
@ ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/sign/Home.vue?vue&type=script&lang=js&
@ ./src/views/sign/Home.vue?vue&type=script&lang=js&
@ ./src/views/sign/Home.vue
@ ./src/router/index.js
@ ./src/js/core/init.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://192.168.43.252:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
How can we resolve this issue??
Activity
sairamsunkara673M commentedon Apr 8, 2021
Node version "12.16"
pdfjs-dist version "^2.7.570"
Vue Version "^2.6.11",
OS - Ubuntu - Version 18
Snuffleupagus commentedon Apr 8, 2021
Unfortunately "Vue", whatever that is, isn't something that the PDF.js contributors can support.
Furthermore, there's not really any actionable information provided as-is; please note https://github.com/mozilla/pdf.js/blob/master/.github/CONTRIBUTING.md (emphasis mine):
You probably need to use the correct type of build then, as explained at https://www.npmjs.com/package/pdfjs-dist/v/2.7.570?activeTab=readme
laazaz commentedon Apr 8, 2021
Same problem in React, after upgrading pdfjs-dist to 2.7.570
hanqizheng commentedon Apr 9, 2021
Same problem in React
"pdfjs-dist": "^2.2.228"
"react": "^16.8.6",
node: v14.16.1
npm: 6.14.12
Snuffleupagus commentedon Apr 9, 2021
The answer has already been provided in #13200 (comment), so unless you explain what's unclear about it we cannot help!
That version is no longer supported, please find the supported versions at e.g. https://mozilla.github.io/pdf.js/getting_started/#download
hanqizheng commentedon Apr 9, 2021
I locked the version to 2.2.228 instead of "^2.2.228" then this error didn't show any more.
Kind of weird solution but it's effective.
I'll find a new version to update current version, thank you answerd this question so quickly. 😁
Snuffleupagus commentedon Apr 9, 2021
As clearly mentioned #13200 (comment), that version is explicitly unsupported.
Please update to a supported release, and simply follow the already provided information which tells you to import from
node_modules/pdfjs-dist/es5/build/pdf.js
instead.MaxGeldner commentedon Apr 9, 2021
The issue is, that in some version after 2.5 some lines with the optional chaining operator were added. This operator is not supported by webpack by default in its current version (as far as I know). Vue uses webpack (@sairamsunkara673M is using Vue and therefore webpack). So you may need an addiotional loader for the pdfjs-dist files. You could configure your webpack as follows:
This will use the babel plugin-proposal-optional-chaining plugin to compile files with optional chaining operators. Note that you need to have babel installed, in my project I have following babel packages:
Also this will probably extend your build time by a bit, since you need to parse the pdfjs-dist files with an additional loader.
@hanqizheng solution works, because their included version doesn't seem to use the optional chaining operator.
Some other suggestions?
This workaround is not neccessary anymore when webpack adds optional chaining operator support.
Edit:
I additionally needed to add babel-polyfill to my entrypoint in webpack as:
timvandermeij commentedon Apr 9, 2021
If you import the ES5 version as indicated above there should be no optional chaining operators since those are not an ES5 feature. Moreover, we use Webpack 5 and that in turn uses Acorn 8 with according to webpack/webpack#10227 even supports optional chaining, so also here I don't see why this should not work. Just make sure you're using the latest PDF.js release since older ones may have been built with Webpack 4.
elimence commentedon Jul 7, 2021
I'm using react and I'm having this same problem with all the versions I have tried so far except for
2.2.228
There's no
es5
directory thoughdavidbstein commentedon Sep 27, 2021
Since this is the first search result for the error message and the solution isn't in the thread...
Symptom: The following error message shows up when you build.
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file
Cause: your build configuration does not support option chaining. Likely something with webpack or babel.
Solution 1: the quick fix is to use the
legacy
build folder.Solution 2: Change your webpack configuration to support option chaining by installing
@babel/preset-env
or following the instructions in the comments earlier in this thread.1 remaining item
blacelle commentedon Oct 23, 2021
With vue-cli, it may be a bit more difficult to configure webpack. We succeeded by adding:
In vue.config.js (https://cli.vuejs.org/core-plugins/babel.html):
in babel.config.js:
czyzby commentedon Dec 4, 2021
The
transpileDependencies
didn't fully work for me. While I eventually managed to render a page,getTextContent
returned no items without any errors. Switching to the legacy build fixed the issue.qiubinyang commentedon Jan 26, 2022
I meet the same problem. Donnot use cnpm or yarn. Try to delete
node_modules/
and runnpm install
again.akume commentedon Feb 9, 2022
the same is the case for the core version. if you do not want to go through the hassle of upgrading webpack to include optional chaining, point to the legacy version which is included with the dist. the documentation omits that this exists
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf.js'
instead of
import * as pdfjs from 'pdfjs-dist'
kendesha commentedon Sep 22, 2022
This here helped me resolve the issue
stackoverflow
evanjmg commentedon Jan 4, 2023
Why not just revert the offending code? This issue is not resolved. These are all workarounds
davidfunk13 commentedon Feb 15, 2023
Agreed. I am using this for work, and there is not really adequate documentation. Some explicit fixes for React projects would be appreciated.
Laulesage commentedon May 4, 2023
For some reason, pdf js through CDN using const pdfjsLib = window['pdfjs-dist/build/pdf'] , pdfjsLib.GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.js'
worked without issue for a year, but about a month ago its starting to be a coinflip to work in Chrome...
It is working stable in Firefox, but other things are not working then ....
Trying to import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.js' or even import * as pdfjs from 'pdfjs-dist' gives me the same error as above .....
Im about to hang myself ngl...