导入 mp3 时出现“找不到模块”错误

Bin*_*men 3 typescript webpack next.js

我在 NextJS 应用程序中使用 useSound,并且我想播放位于 /public/static 中的 mp3:

import notification from "../../public/static/notification.mp3"
Run Code Online (Sandbox Code Playgroud)

它在运行时工作yarn dev(正在播放声音,没有错误),但在使用以下命令构建时会引发错误yarn prod

Type error: Cannot find module '../../public/static/notification.mp3' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?我一直在浏览 internet/SO/github,并尝试了几件事,包括修改next.config.js

Type error: Cannot find module '../../public/static/notification.mp3' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)

没有成功..

Bin*_*men 5

好吧,显然就这么简单:更新next-env.d.ts

declare module '*.mp3' {
  const src: string;
  export default src;
}
Run Code Online (Sandbox Code Playgroud)

如果有人能解释它是如何工作的,那就太好了。

  • 您忘了提及您正在使用 TypeScript。TypeScript 需要针对不同类型的文件进行类型声明。 (2认同)