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 $path #185

Closed
dalanmiller opened this issue Aug 5, 2020 · 6 comments
Closed

Error: Cannot find module $path #185

dalanmiller opened this issue Aug 5, 2020 · 6 comments

Comments

@dalanmiller
Copy link

Having an issue I think with getting the plugin working as this is a valid file at public/images/products/6607330/1.jpg. I assume based on assetConfig which I haven't changed that the path I'm providing will be concatenated to public.

Anything I can sanity check here?

// next.config.js 
const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
  optimizedImages,
]);

Screen Shot 2020-08-05 at 11 07 05 pm

@dalanmiller dalanmiller changed the title Unable to load module Error: Cannot find module $path Aug 5, 2020
@Vincent-3DAT
Copy link

try require('public/images/'+src)
if that doesnt work try a filepath that is relative to _document

@dalanmiller
Copy link
Author

try require('public/images/'+src)

No good ;(

if that doesnt work try a filepath that is relative to _document

Tried the path relative to _document (../public/images/products/1234/1.jpg) but also still not working.

@cyrilwanner
Copy link
Owner

You need at least one static path before your variable in a require statement, this is a limitation of webpack: https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import

Also, your path is relative to your current file. It works exactly the same as when you would import another js file.

@dalanmiller
Copy link
Author

dalanmiller commented Aug 5, 2020

What a bizarre limitation! But makes sense that the less specific you are, the more files WebPack will need to load to ensure that your file will be available to import.

I've modified this so that the require( has the static path up front and then the prop I pass in is just the folder name and filename in the path. However, it still seems to be failing for the same reasons. Unclear why the error isn't showing the full path and instead just the latter part?

e.g. src = '6450868/1.jpg'

Screen Shot 2020-08-06 at 8 53 53 am

Side note: Is there a manual or different way to trigger image optimization other than using require?

edit:

const webpImage = require(`../public/images/products/${src}?webp`)

Also fails.

@dalanmiller
Copy link
Author

Same as #16

@dalanmiller
Copy link
Author

Issue here was jpg files with capitalised extensions and not lowercase 🤦‍♂

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

3 participants