Skip to content

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

Closed
@sairamsunkara673M

Description

@sairamsunkara673M
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

sairamsunkara673M commented on Apr 8, 2021

@sairamsunkara673M
Author

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

Snuffleupagus

Snuffleupagus commented on Apr 8, 2021

@Snuffleupagus
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

laazaz commented on Apr 8, 2021

@laazaz

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

hanqizheng

hanqizheng commented on Apr 9, 2021

@hanqizheng

Same problem in React

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

Snuffleupagus

Snuffleupagus commented on Apr 9, 2021

@Snuffleupagus
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

hanqizheng commented on Apr 9, 2021

@hanqizheng

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

Snuffleupagus commented on Apr 9, 2021

@Snuffleupagus
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

MaxGeldner commented on Apr 9, 2021

@MaxGeldner

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

timvandermeij commented on Apr 9, 2021

@timvandermeij
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

elimence commented on Jul 7, 2021

@elimence

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

deleted a comment from PLQin on Jul 14, 2021
davidbstein

davidbstein commented on Sep 27, 2021

@davidbstein

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.

1 remaining item

blacelle

blacelle commented on Oct 23, 2021

@blacelle

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

czyzby commented on Dec 4, 2021

@czyzby

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

qiubinyang commented on Jan 26, 2022

@qiubinyang

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

akume commented on Feb 9, 2022

@akume

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

kendesha commented on Sep 22, 2022

@kendesha

This here helped me resolve the issue
stackoverflow

evanjmg

evanjmg commented on Jan 4, 2023

@evanjmg

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

davidfunk13

davidfunk13 commented on Feb 15, 2023

@davidfunk13

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

Laulesage

Laulesage commented on May 4, 2023

@Laulesage

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @irskep@akume@davidbstein@timvandermeij@blacelle

        Issue actions

          You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file · Issue #13200 · mozilla/pdf.js