1

So I am using Next.js API for the backend of my project and Mongodb for the database. I have to serve images which have been uploaded by users on the runtine. As these files weren't available yet during the build time, they are not recognized as static files which can be stored in public directory.

I have tried next-images, added some configurations in the next.config.js and in my code: <img src={require(../../../images/${picture.src})} alt={picture.legend} />.

But still I have this error:Module parse failed.

I also tried with file-loader but still got no luck.

What did I miss?

Thanks for any reply!

2 Answers 2

1

This configuration has worked for me in the past when running into these issues with loading images and/or fonts:

  1. npm install url-loader --save-dev

  2. next.config.js

module.exports = {
  webpack: function (config) {
    config.module.rules.push({
      test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
      use: {
        loader: 'url-loader',
        options: {
          limit: 100000,
          name: '[name].[ext]',
        },
      },
    });
    return config;
  },
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @james but unfortunatelly, it didn't solve the issue. I still get the same error.
Try changing the extension of your image from .JPG to .jpg. Both the regex I posted as well as next-images expects a lowercase extension on your file.
Thank you, yes that solved the issue related to loading the images. Now the images are shown as they are stored in public folder. However when I build the project and upload new images afterwards, all these new images are still not found even though they are located in the same directory as the previously added ones.
@HoracioSoldman did you find a solution to this?
@TigerCode Unfortunately, the way I got around with it was to create a very basic express server which only serves the dynamic images. Probably this issue was fixed in recent versions of Next.js.
0

The public folder is only used to store static content, and not dynamic content. That's why when you rebuild the files will be available.

Your qestion answered from Github

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.