Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't reference an image in Next.js

Tags:

I have a React component that's being used in Next.js page:

/pages/index.js

import React from 'react'; import ReactDOM from 'react-dom'; import Layout from "../src/hoc/Layout/Layout"; import Main from "../src/components/Main/Main";  const Index = () => (    <Layout>        <Main />    </Layout> ); export default Index 

In Main.js I have the following code

import macbookIphone from '../../assets/images/mac-iphone.jpg'; 

I get the following error

Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders (Source code omitted for this binary file)

I tried doing the following

In next-config.js

const withImages = require('next-images') module.exports = withImages() 

I'm still getting the same error.

What am I doing wrong?

like image 404
Pota Onasys Avatar asked Dec 31 '19 16:12

Pota Onasys


People also ask

Can I use IMG in NextJS?

There are two ways you can display images in Next. js, you either use the conventional <img> tag or a specialized <Image/> component that is unique to Next.

Where should I put images in NextJS?

Next. js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced by your code starting from the base URL ( / ).

Does NextJS lazy load images?

Lazy loading is enabled by default in the Next. js Image component. It is not only useful to the end-user, but it is also beneficial to the server because it does not have to execute image generation for images that may or may not be required.


2 Answers

Please see https://nextjs.org/docs/basic-features/static-file-serving

Next.js can serve static files, like images, under a folder called public in the root directory. Files inside public can then be referenced by your code starting from the base URL (/).

like image 180
Blake Plumb Avatar answered Oct 12 '22 20:10

Blake Plumb


/public/favicon/mail.png

=> "/favicon/mail.png" will work

like image 31
2donny Avatar answered Oct 12 '22 20:10

2donny