Skip to content
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

You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file #13200

Closed
sairamsunkara673M opened this issue Apr 8, 2021 · 20 comments

Comments

@sairamsunkara673M
Copy link

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??
@sairamsunkara673M
Copy link
Author

sairamsunkara673M commented Apr 8, 2021

Node version "12.16"
pdfjs-dist version "^2.7.570"
Vue Version "^2.6.11",
OS - Ubuntu - Version 18

@Snuffleupagus
Copy link
Collaborator

Vue Version "^2.6.11",

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):

If you are developing a custom solution, first check the examples at https://github.com/mozilla/pdf.js#learning and search existing issues. If this does not help, please prepare a short well-documented example that demonstrates the problem and make it accessible online on your website, JS Bin, GitHub, etc. before opening a new issue or contacting us in the Matrix room -- keep in mind that just code snippets won't help us troubleshoot the problem.


error in ./node_modules/pdfjs-dist/build/pdf.js

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

For usage with older browsers or environments, without support for modern features such as e.g. async/await, ReadableStream, optional chaining, and nullish coalescing; please see the es5 folder.

@laazaz
Copy link

laazaz commented Apr 8, 2021

Same problem in React, after upgrading pdfjs-dist to 2.7.570

@hanqizheng
Copy link

Same problem in React

"pdfjs-dist": "^2.2.228"
"react": "^16.8.6",
node: v14.16.1
npm: 6.14.12

@Snuffleupagus
Copy link
Collaborator

Same problem in React, after upgrading pdfjs-dist to 2.7.570

The answer has already been provided in #13200 (comment), so unless you explain what's unclear about it we cannot help!


Same problem in React
"pdfjs-dist": "^2.2.228"

That version is no longer supported, please find the supported versions at e.g. https://mozilla.github.io/pdf.js/getting_started/#download

@hanqizheng
Copy link

hanqizheng commented 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
Copy link
Collaborator

I locked the version to 2.2.228 instead of "^2.2.228" then this error didn't show any more.

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
Copy link

MaxGeldner commented 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:

module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules(?!(\/|\\)pdfjs-dist)/,
                loader: 'babel-loader',
                options: {
                    'presets': ['@babel/preset-env'],
                    'plugins': ['@babel/plugin-proposal-optional-chaining']
                }
            }
        ],
    },

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:

"@babel/core": "^7.13.15",
"@babel/preset-env": "^7.13.15",
"babel-loader": "^8.2.2",

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:

entry: {
        main: ['babel-polyfill', path.join(rootPath, '/src/app/main.js')],
}

@timvandermeij
Copy link
Contributor

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
Copy link

elimence commented Jul 7, 2021

I locked the version to 2.2.228 instead of "^2.2.228" then this error didn't show any more.

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.

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 though

@mozilla mozilla deleted a comment from PLQin Jul 14, 2021
@davidbstein
Copy link

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.

// If this causes an error:
import * as PDFJSViewer from "pdfjs-dist/web/pdf_viewer";
import {getDocument } from "pdfjs-dist/build/pdf.js";

// use this instead:
import * as PDFJSViewer from "pdfjs-dist/legacy/web/pdf_viewer";
import {getDocument } from "pdfjs-dist/legacy/build/pdf.js";

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.

@irskep
Copy link

irskep commented Sep 30, 2021

@davidbstein's Solution 1 worked great for me as a Parcel user. Too bad these different versions aren't documented in any examples or the Webpack instructions.

@blacelle
Copy link

blacelle commented 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):

transpileDependencies: [
    'pdfjs-dist'
  ]

in babel.config.js:

module.exports = {
  presets: ['@vue/app', '@babel/preset-env'],
  plugins: ["@babel/plugin-proposal-optional-chaining"],
};

@czyzby
Copy link

czyzby commented 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
Copy link

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.

I meet the same problem. Donnot use cnpm or yarn. Try to delete node_modules/ and run npm install again.

@akume
Copy link

akume commented 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
Copy link

kendesha commented Sep 22, 2022

This here helped me resolve the issue
stackoverflow

@evanjmg
Copy link

evanjmg commented Jan 4, 2023

Why not just revert the offending code? This issue is not resolved. These are all workarounds

@davidfunk13
Copy link

Agreed. I am using this for work, and there is not really adequate documentation. Some explicit fixes for React projects would be appreciated.

@Laulesage
Copy link

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests