网站搜索

Typescript pdfjs-dist:您可能需要一个额外的加载器


我正在尝试使用 @react-pdf-viewer/[email 实现 PDF 查看器,该库依赖于 [email 。我已经安装了这两个库,但我收到以下错误:

./node_modules/pdfjs-dist/build/pdf.js 1259:21
    Module parse failed: Unexpected token (1259:21)
    File was processed with these loaders:
     * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
    You may need an additional loader to handle the result of these loaders.
    | 
    |         class PixelsPerInch {
    >           static CSS = 96.0;
    |           static PDF = 72.0;
    |           static PDF_TO_CSS_UNITS = this.CSS / this.PDF;

我已经尝试删除并重新安装 node_modules,但完全不起作用。我没有 babel-loader。我使用的是 "typescript": "^4.1.2","react": "^17.0.2"。我该如何解决这个问题?

答案#1

从错误中,我可以看出你正在使用 react-scripts(顺便说一下,这也是 babel-loader 的来源——你确实在使用它,它是一个隐式依赖)。

我今天遇到了完全相同的错误。在我的情况下,我试图将 storybook 添加到一个现有的 react-scripts 项目中。项目本身构建正常,但 storybook 构建出现了您的错误。

我最终发现,添加这个 webpack 规则就足以让我的构建工作正常运行。它是 react-scripts 对外部包代码所做操作的简化版本(我是通过 eject 并查看生成的文件找到的)。

          {
            test: /\/pdfjs-dist\//,
            loader: require.resolve('babel-loader'),
            options: {
              presets: [
                [
                  // Latest stable ECMAScript features
                  require('@babel/preset-env').default,
                ],
              ],
            },
          },

希望这对你有用!