【问题标题】:React Nextjs: Module parse failed:You may need an appropriate loader to handle this file type,currently no loaders are configured to process this fileReact Nextjs:模块解析失败:您可能需要适当的加载器来处理此文件类型,当前没有配置加载器来处理此文件
【发布时间】:2022-01-22 20:44:25
【问题描述】:

我正在尝试加载 pdf 文件

import placeholder from "../assets/pdf/placeholder.pdf"

每次服务器重新加载时,我都会收到此错误: * 错误 - ./assets/pdf/placeholder.pdf 模块解析失败:意外令牌 (1:0) 您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。见https://webpack.js.org/concepts#loaders (此二进制文件的源代码省略)*

【问题讨论】:

    标签: reactjs webpack next.js


    【解决方案1】:

    如果您使用的是 Webpack4,您似乎需要raw-loader

    https://v4.webpack.js.org/loaders/raw-loader/

    {
      test: /\.pdf$/i,
      use: 'raw-loader',
    }
    

    如果您使用的是 Webpack5,则可以使用新的资产模块:

    https://webpack.js.org/guides/asset-modules/

    (原始加载器替换)

    {
      test: /\.pdf$/i
      type: 'asset/source'
    }
    

    (文件加载器替换)

    {
      test: /\.pdf$/i
      type: 'asset/resource',
      generator: {
        filename: `[name][ext]`
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-30
      • 2022-10-18
      • 1970-01-01
      • 2019-10-11
      • 2018-02-27
      • 1970-01-01
      • 2021-08-11
      • 2020-01-15
      相关资源
      最近更新 更多