next.js: sharp no longer optional since 10.0.4-canary.7

Bug report

Describe the bug

Next.js has an optional dependency on sharp. This means that it should run without sharp present in node_modules.

PR #20035 broke this by adding require.resolve('sharp') without a try/catch to catch MODULE_NOT_FOUND.

I discovered this because sharp no longer installs with NPM 7 (see #20432).

To Reproduce

npm i next@10.0.4-canary.7 rimraf node_modules/sharp npm run build

Build error occurred Error: Cannot find module ‘sharp’ Require stack:

  • C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\build\index.js
  • C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\cli\next-build.js
  • C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\bin\next at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.resolve (internal/modules/cjs/helpers.js:94:19) at build (C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\build\index.js:11:794) { code: ‘MODULE_NOT_FOUND’, requireStack: [ ‘C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\build\index.js’, ‘C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\cli\next-build.js’, ‘C:\Projects\ANWB\Travelhome-git\camper2-website\node_modules\next\dist\bin\next’ ] }

Expected behavior

The site builds OK.

  • OS: Windows or Linux (alpine in docker)
  • Version of Next.js: 10.0.4-canary.7 and up
  • Version of Node.js: 14 (latest)
  • Deployment: fails on build

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 28
  • Comments: 23 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Downgrading to {"next": "10.0.3"} worked wonders for me

@robertveloso A new stable version hasn’t yet been released since the fix, but if you install the latest preview version (currently 10.0.8-canary.5) you’ll find it fixes the issue 😃

For macos users: brew install libvips and voila! For debian users: apt install libvips approximate answer, I dunno how is it called on debian & arch package managers…

Also, don’t forget to upgrade it if it exists already!

@genemators If only that was that easy.

What about corporate environments that just can’t download things from the internet for builds, only from an npm mirror ? That’s the reason this dependency was made optional in the first place.

brew install libvips

Thank you @genemators 😄 now im downloading all the deep web and google server files on my desktop ❤️

Tks for the report and PR @jorrit !

npm i next@10.0.4-canary.7

I don’t really know why but this happens in 10.0.4 (the latest version, not the canary one) too

image

@genemators If only that was that easy.

What about corporate environments that just can’t download things from the internet for builds, only from an npm mirror ? That’s the reason this dependency was made optional in the first place.

I dunno dude, this is just a temporary solution.

thank you much brother! unfortunately, it didn’t work. I spent 8 hours trying to make this work on the beanstalk. no luck went back to vercel and it worked in less than a minute. hopefully, there is official a guide for aws

Doesn’t work in mac.

For Arch Linux (or Arch Linux based distro likes Manjaro), try sudo pacman -S libvips

brew install libvips

Thank you @genemators 😄 now im downloading all the deep web and google server files on my desktop ❤️

Bruh, it was just an option …

Any update on this? This introduces so many new dependencies that previous docker images fail to build because this now requires sharp which reuqires python3 and so on. This currently prevents us from upgrading to the latest next.js version

I’m seeing this too. Everything builds fine on Windows and Mac, but fails when we deploy.

OS: Amazon Linux 2 Node: 12.20.0 npm: 6.14.8 Next: 10.0.4

Perhaps a CI test that removes sharp after installing the dependencies would prevent this in the future.