question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static import of images using next/image not loading any images.

See original GitHub issue

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

16.8.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next dev

Describe the Bug

I’m using next/image in the project. It doesn’t load image when using this mentioned static import method. Have a look at the code of pages/test.js file.

import Image from "next/image";
import myLocalImage from "../public/images/shape.png";

const testPage = () => {
  return (
      <>
        <Image 
            src={myLocalImage} 
            alt="image 1" 
        />
        <hr/>
        <Image 
            src="/study/images/shape.png" 
            alt="image 2" 
            width="99px"
            height="195px"
        />
      </>
  );
};

export default testPage;

Note here that I’m using the basePath config with value : { basePath: '/study'} in next.config.js. And it produces the output:

Screenshot 2021-10-15 at 3 24 17 PM

Inspecting the network tab, i found the server is not giving the image (as shown in the following screenshot)

Screenshot 2021-10-15 at 2 57 01 PM

If I had to guess, I’d say because I’m using the basePath config, I think the server is not able to respond in the first case of static import! I have tried this in a dev environment - next dev and also in next start (after building first.)

For reference, I’m also posting the decoded URLs of both images here : image-1 (used by static import): http://localhost:3101/study/_next/image/?url=/_next/static/image/public/images/shape.97a1a3c616d56b9fdca81af284112b9e.png&w=256&q=75 and for image 2 (used by inline src) : http://localhost:3101/study/_next/image?url=/study/images/shape.png&w=256&q=75

Let me know if this is a known issue or I’m making any mistakes!?

Expected Behavior

The first image should load perfectly.

To Reproduce

In the next.config.js file, set the basePath option like I have:

{
    basePath: "/study",
}

Then create a page : pages/test.js, paste the following code:

import Image from "next/image";
import myLocalImage from "../public/images/shape.png";

const testPage = () => {
  return (
      <>
        <Image 
            src={myLocalImage} 
            alt="image 1" 
        />
        <hr/>
        <Image 
            src="/study/images/shape.png" 
            alt="image 2" 
            width="99px"
            height="195px"
        />
      </>
  );
};

export default testPage;

And add shape.png in the public/images directory. That’s it.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
darshantejani007commented, Jan 1, 2022

Yes, After updating to v 12.0.7 and making changes in my next.config.js file as per the following comment, I managed to solve this.

https://github.com/vercel/next.js/issues/19711#issuecomment-750414110

0reactions
balazsorban44commented, Jan 31, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nextjs: Unable to load images from static folder - Stack Overflow
Next.js can serve static files, like images, under a folder called public in ... import Image from 'next/image' import profilePic from '.
Read more >
Working with images in Next.js - DEV Community ‍ ‍
If you're rendering images or any media files relatively ( locally ) in your project, you must store them in the /public folder...
Read more >
next/image - Next.js
In many cases, it's better to use a Static Image Import which will automatically hash the file contents and cache the image forever...
Read more >
How to Import Images in Next JS - YouTube
In this video I go over how to import images in Next JS. Next JS offers some really great features right out of...
Read more >
How to add an image in next.js? - Medium
js adds lazy load and eager is not good for image optimization. import Image from 'next/image' import mypic from '../asset/myimage.png' const ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found