如何配置next.js以从外部依赖项加载静态文件?

如何配置next.js以从外部依赖项加载静态文件?,next.js,Next.js,我遇到了以下问题。我好像错过了一些重要的东西。谁能给我指个正确的方向吗 情况与此非常相似 我的应用程序使用一个外部组件,在其代码中导入和渲染图像。看起来是这样的: const React=require(“React”); const ico=要求(“./favicon.ico”); module.exports={ Img:()=>React.createElement(“Img”,{src:ico.default}), }; 我已经在我的中为它配置了url加载程序: module.expo

我遇到了以下问题。我好像错过了一些重要的东西。谁能给我指个正确的方向吗

情况与此非常相似

我的应用程序使用一个外部组件,在其代码中导入和渲染图像。看起来是这样的:

const React=require(“React”);
const ico=要求(“./favicon.ico”);
module.exports={
Img:()=>React.createElement(“Img”,{src:ico.default}),
};
我已经在我的中为它配置了
url加载程序

module.exports={
网页包(配置、选项){
config.module.rules.push({
测试:/.\.ico$/,,
加载器:“url加载器”,
});
返回配置;
},
};
但当我从node_模块导入该组件时,它似乎不起作用。但是,我可以通过其局部相对路径导入相同的组件(请参见repro),并且工作正常

我已经准备好了一个小程序。该组件可以正常工作。你可以在那里找到两个相等的进口。第一个有效(注释),但第二个在运行
dev
/
build
命令后访问服务器url时出错:

$ next dev
ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /
wait  - compiling...
event - compiled successfully
C:\xxx\next-files-demo\node_modules\next-files-demo-dep\favicon.ico:1


SyntaxError: Invalid or unexpected token
    at compileFunction (<anonymous>)
    at wrapSafe (internal/modules/cjs/loader.js:1070:16)
    at Module._compile (internal/modules/cjs/loader.js:1120:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Module.require (internal/modules/cjs/loader.js:1042:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (C:\xxx\next-files-demo\node_modules\next-files-demo-dep\index.js:2:13)
    at Module._compile (internal/modules/cjs/loader.js:1156:30)
event - build page: /next/dist/pages/_error
$next dev
已在上准备好启动服务器http://localhost:3000
事件-已成功编译
事件-生成页面:/
等待-编译。。。
事件-已成功编译
C:\xxx\next files demo\node\u modules\next files dem dep\favicon.ico:1
SyntaxError:无效或意外的令牌
在编译函数中(
  • 运行
    纱线
  • 运行纱线开发
  • 转到
    http://localhost:3000
  • 看到错误了吗
  • 我不明白为什么会这样

    您还可以在Next.js的github中启动的文件中看到生成的网页包配置

    PS:我知道这个方法,但由于问题是来自外部部门,我不知道它能帮我什么忙