Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Error when importing sharp package - Nextjs Image Blur Placeholder Hash

default discord avatar
reepicheep05last year
9

I'm working on a hook to generate blur hashes when an image is uploaded so I can use them with the Nextjs Image component.



It is pretty simple to generate the blur, but for some reason I keep getting an error when I try to use the

sharp

library. It looks like it is bundled in with Payload, so I'm not sure if there is anything different that I need to do other than importing it into my hook.



The issue seems to happen on this line in the

sharp

library.



// node_modules/detect-libc/lib/detect-libc.js
const childProcess = require('child_process');


The console shows:


Module not found: Error: Can't resolve 'child_process' in '/Users/.../node_modules/detect-libc/lib'


I've tried adding

sharp

to my project, but that didn't seem to help. Uninstalled that package and reinstalled Payload and keep getting the same error. Might just have to use another library, but I would like to use this one since it works well and is already included.



It might be making it into the client bundle. Trying this alias...

https://payloadcms.com/docs/admin/webpack#aliasing-server-only-modules
  • 100%



    That’s the issue

  • default discord avatar
    reepicheep05last year

    Heck yeah! the docs make this look really easy, but I'm still on it. Not out of the woods yet haha

  • What’s not working for ya? What’s your webpack property look like in your payload config?

  • default discord avatar
    reepicheep05last year

    I got it working! Thanks though. I was trying to reroute a folder, but my syntax must have been off. I am currently just aliasing a single file and it working great.



    It would be nice to alias a folder all to one mock file.

  • I just don't think thats how it works. When webpack traverses the file tree, it looks at all import paths at the top of files and then compares each one with the ones in your resolve.alias and if it finds an alias route, it uses that instead



    But now you know how to fix it and will recognize it when others run into it. Glad you were able to solve 🙂

  • default discord avatar
    reepicheep05last year

    Ok that makes sense. Thanks for the help!

  • default discord avatar
    kristijan007vlast year

    Hi, sorry I am late to topic I hope you can replay, how did you manage to solve this issue in the end, I have the same problem and tried everything…



    Thank you in advance

  • default discord avatar
    notchrlast year

    @kristijan007v is sharp making it into the client bundle? it shouldnt

  • default discord avatar
    kristijan007vlast year

    @notchris I think it is, I tried adding it to the externals inside the webpack config for Payload and many other things with no success, I opted in using this library

    https://plaiceholder.co

    after all



    @notchris Thank you for trying to help 🍻

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.