导入 mp3 时出现"Cannot find module"错误



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

import notification from "../../public/static/notification.mp3"

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

Type error: Cannot find module '../../public/static/notification.mp3' or its corresponding type declarations.

有人能帮我吗?我一直在查看互联网/SO/github,并尝试了一些方法,包括修改next.config.js:

webpack(config, { isServer }) {
config.module.rules.push({
test: /.(ogg|mp3|wav|mpe?g)$/i,
exclude: config.exclude,
use: [
{
loader: require.resolve('url-loader'),
options: {
limit: config.inlineImageLimit,
fallback: require.resolve('file-loader'),
publicPath: `${config.assetPrefix}/_next/static/`,
outputPath: `${isServer ? '../' : ''}static/`,
name: '[name]-[hash].[ext]',
esModule: config.esModule || false,
},
},
],
});
return config;
},

没有成功。。

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

declare module '*.mp3' {
const src: string;
export default src;
}

如果有人能解释这是怎么回事,那就太好了。

相关内容

最新更新