在我更新 Next 到版本 11 后,当我尝试加载一张图片时:
import segmentLogoWhitePng from 'assets/images/my-image.png'
我遇到了以下错误:
TypeError: unsupported file type: undefined (file: undefined)
截至于next@v11.0.1
版本,它现在可以运行了。无需执行以下步骤。
暂时将静态图像功能禁用作为解决方法:
// next.config.js
module.exports = {
images: {
disableStaticImages: true
}
}
更新:这已在next@11.0.1-canary.4中修复。安装它:
$ npm install next@canary
禁用静态导入
- 自10.0.0版本以来,Next.js已经内置图像组件和自动图像优化。
默认情况下,您可以导入静态文件,例如import icon from './icon.png',然后将其传递给src属性。在某些情况下,如果此功能与其他插件产生冲突并期望导入的行为不同,则可能希望禁用此功能。您可以使用以下配置禁用静态图像导入。
// next.config.js
images: {
disableStaticImages: true
}
我刚刚从webpack(next.config.js)中删除了我的图像/CSS加载器配置,并且它开始正常工作了。
module.exports = {
images: {
disableStaticImages: true
}
}
next.config.js
中,您可以添加文件类型检查和处理程序。我知道可以通过添加以下代码并下载npm包@svgr/webpack来处理svg,因此可能存在.png的等效物。module.exports = {
webpack(config) {
config.module.rules.push({
test: /\.svg$/,
use: ["@svgr/webpack"],
});
return config;
},
};
一个可行的示例是来自这个 Stack Overflow 的代码。
module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
use: [
{
loader: 'file-loader',
},
],
},
],
}
};
我知道这个答案不是100%,但希望它能对你有所帮助。
^11.1.2
仍然会出现这个错误。 - JohnAllen^12+
中。disableStaticImages
也没有帮助。必须将其移动到public
文件夹并使用相对URL而不是import
。 - Domi^12.1.6
版本。 - avalanche1