简体   繁体   English

Next.js 构建失败使用<img>标签

[英]Next.js build fails using <img> tag

I recently started making websites with Next.js, and I have been using a mix of Image and img for various use cases.我最近开始使用 Next.js 制作网站,并且我一直在将Imageimg混合用于各种用例。

I know that Image is built-in to Next.js and is the better choice, but there are scenarios where I do not know the size or the ratio of the images I am loading, and thus the img seems to better suit.我知道Image内置于Next.js并且是更好的选择,但在某些情况下我不知道我加载的图像的大小或比例,因此img似乎更适合。

During my recent project, this is the first time my npm run build command is failing with:在我最近的项目中,这是我的npm run build命令第一次失败:

1:7  Error: Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.

My other Next.js projects do not fail this way, and use the same next.config.js .我的其他 Next.js 项目不会以这种方式失败,并使用相同的next.config.js Does anyone know why this is happening?有谁知道为什么会这样?

This is due to the new Next.js release, which has integrated ESLint with its own set of rules to enforce that <Image> should always be used.这是由于新的 Next.js 版本将 ESLint 与其自己的一组规则集成在一起,以强制应始终使用<Image> Your other projects don't fail because probably you are not using Next.js v11, or they may have their own ESLint config, where you haven't extended next .你的其他项目不会失败,因为你可能没有使用 Next.js v11,或者他们可能有自己的 ESLint 配置,而你没有扩展next Ref.: nextjs.org/docs/basic-features/eslint参考: nextjs.org/docs/basic-features/eslint

You can ignore linting during build by this:您可以通过以下方式在构建过程中忽略掉毛:

// next.config.js

module.exports = {
  eslint: { ignoreDuringBuilds: true },
  // your other settings here ...
}

If you want to specifically disable the rule for a file or just a line do this: (can be done in two clicks if using ESLint extension in VSCode)如果你想专门禁用文件的规则或只是一行,请执行以下操作:(如果在 VSCode 中使用 ESLint 扩展,可以单击两次完成)

{/* eslint-disable-next-line @next/next/no-img-element */}
<img ... //>

// for whole file, add this to top:

/* eslint-disable @next/next/no-img-element */

// for a section:

/* eslint-disable @next/next/no-img-element */
// your code here...
/* eslint-enable @next/next/no-img-element */

You can also disable the rule using .eslintrc :您还可以使用.eslintrc禁用规则:

{
  "extends": "next",
  "rules": {
    "@next/next/no-img-element": "off",
  }
}

You can also ignore (all rules for) a complete file using eslintignore or ignorePatterns .您还可以使用eslintignoreignorePatterns忽略(所有规则)完整文件。 Please refer: eslint.org/docs/user-guide/configuring/ignoring-code请参考: eslint.org/docs/user-guide/configuring/ignoring-code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM