Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module in build process #203

Open
AldoS opened this issue Sep 8, 2020 · 4 comments
Open

Error: Cannot find module in build process #203

AldoS opened this issue Sep 8, 2020 · 4 comments

Comments

@AldoS
Copy link

AldoS commented Sep 8, 2020

Hi guys,
I have an issue with the build process.
What I am trying to do, is to save some images from API to my project. I am using the file stream in the getStaticProps function to achieve that.

Then in my page, I require the image that is saved previously:
<img src={require('../../../public/images/hero/my-image.jpg?webp')} />

In my first build, I get the error: Error: Cannot find module './my-image.jpg'
If I build again everything works fine because the image is already there.

I tried to check if the image exists before the return in getStaticProps using fs.existsSync(file) and my file exists.

Any ideas on the above error or any hints? Thanks

@oknoorap
Copy link

Same with me, can't build svg sprite

@andree-rehnberg
Copy link

andree-rehnberg commented Oct 11, 2020

Also getting this. Using fs in getStaticProps to fetch images from api and save to ./public/api_images/, when I then use require('../public/api_images/my_image.png') I get an error saying Error: Cannot find module './api_images/my_image.png'

Edit: To clarify, the project build just fine and is able to find all images if use a regular string as src for the <img> tag instead of the require() that this package calls for.

@ka8725
Copy link

ka8725 commented Oct 31, 2020

I've fixed this project with alias:

module.exports = withFonts(
  withOptimizedImages(
    {
      webpack: (config) => {
        config.resolve.alias['@public'] = path.resolve(__dirname, 'public');
        return config;
      },
    }
  )
);

Then you can do <img src={require('@public/images/hero/my-image.jpg?webp')} />

@wadhah101
Copy link

having the same issue with nextJS 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants