54

I know that in Next.js we have Image component, but the problem I have with it is that I can't use it as a normal HTML tag like <img />. I have to give it a layout, but there's no option to control it as a normal HTML tag, and besides that, I can't use framer motion with it.

So I just installed next-images so I can import images and use them of course, and everything works fine, 'till I npm run build the landing page to see some results, and there's this warning:

Failed to compile.

./components/Presentation/Presentation.js
77:13  Error: Do not use <img>. Use Image from 'next/image' instead. See https://nextjs.org/docs/messages/no-img-element.  @next/next/no-img-element

Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules

npm ERR! code ELIFECYCLE

This is where I'm using img tag with styled components:

<PresentationUnderText2 src="/underText-Presentation.png" alt="" />
<PresentationScissor2   src="/scissors.svg"alt=""/>

What can I do to use img tag as normal?

4 Answers 4

148

From Next.js 11, ESLint is supported out-of-the-box and a new set of rules is now provided, including the @next/next/no-img-element rule.

You can disable this specific ESLint rule, like any other rule, in your .eslintrc file.

{
  // ...
  "rules": {
    // Other rules
    "@next/next/no-img-element": "off"
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For me, I had to reload VS Code or Restart Extension Host to get the warning to disappear.
17

I do not have .eslintrc file. instead I place this line at the top of the page or component

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

Comments

11

If you want to use img tag in your project, you can disable the no-img-element ESLint rule that is causing the error by adding the following configuration to your .eslintrc.js file:

module.exports = {
  rules: {
   "@next/next/no-img-element": "off",
  },
};

Although, I would recommend using the Image component instead as it provides several benefits such as automatic optimization, responsive images, and better performance.

To use framer-motion with the Image component, you can wrap it in a motion component like this:

import Image from 'next/image';
import {motion} from 'framer-motion';

const componentName = () => {
  return (
   <motion.div>
     <Image src="/my-image.png" alt="my-image" width={500} height={500} />
   </motion.div>
  );
};

Comments

3

This answer is very similar to the others provided but gives the full configuration file since Next.js and Eslint moved to the flat configuration file.

This is what I use w/a Next.js + TypeScript project.

// eslint.config.mjs
import { dirname } from "path";
import { fileURLToPath } from "url";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const compat = new FlatCompat({
  baseDirectory: __dirname,
});

const eslintConfig = [
  ...compat.extends("next/core-web-vitals", "next/typescript"),
  {
    rules: {
      "@next/next/no-img-element": "off",
    },
  },
];

export default eslintConfig;

And a link to the eslint documentation for the configuration file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.