qwik: [🐞] Error: Could not load the "sharp" module using the linux-x64 runtime


Summary

This issue is due to npm not storing optional platform-dependent dependencies in the lockfile (see issue), so that using the lockfile on another platform fails. pnpm doesn’t seem to have a problem with it.

Leaving this open for visibility.


Which component is affected?

Qwik Runtime

Describe the bug

After updating Qwik to the latest version, I’m getting the following error when trying to build an app:

error during build:
Error: Could not load the "sharp" module using the linux-x64 runtime
Possible solutions:
- Add platform-specific dependencies:
    npm install --os=linux --cpu=x64 sharp
  or
    npm install --force @img/sharp-linux-x64
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
    at Object.<anonymous> (/home/przemek/dev/personal/project-starter-graphql/frontend/node_modules/@builder.io/qwik-city/node_modules/sharp/lib/sharp.js:85:9)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/home/przemek/dev/personal/project-starter-graphql/frontend/node_modules/@builder.io/qwik-city/node_modules/sharp/lib/constructor.js:10:1)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
error Command failed with exit code 1.

It could be related to this issue: https://github.com/lovell/sharp/issues/3870

Reproduction

https://github.com/Przemoo16/qwik-bug

Steps to reproduce

  1. yarn create qwik
  2. yarn build

System Info

System:
    OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
    Memory: 26.08 GB / 31.09 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 20.5.1 - ~/.nvm/versions/node/v20.5.1/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v20.5.1/bin/yarn
    npm: 9.8.0 - ~/.nvm/versions/node/v20.5.1/bin/npm
  Browsers:
    Brave Browser: 119.1.60.118
  npmPackages:
    @builder.io/qwik: ^1.3.0 => 1.3.0 
    @builder.io/qwik-city: ^1.3.0 => 1.3.0 
    undici: ^5.28.2 => 5.28.2 
    vite: ^5.0.7 => 5.0.7

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 7 months ago
  • Comments: 32 (3 by maintainers)

Most upvoted comments

Next person with issues please try pnpm dedupe, or when using npm, making sure it’s npm>v9.6.5

I did the pnpm dedupe but it didn’t work and I added the above in my package.json

"optionalDependencies": {
    "@img/sharp-darwin-arm64": "^0.33.0"  
  },

Next person with issues please try pnpm dedupe, or when using npm, making sure it’s npm>v9.6.5

@nelsonprsousa #5556 (comment)

We tried with that as well. I believe the problem was with the Dockerfile using yarn v1.

But yeah, no longer using v1. No point really in using v1 in 2024

@wmertens Thanks, you’ve inspired me to add a runtime version check via commit https://github.com/lovell/sharp/commit/45e8071599772f320d0d8314fedf0b13b0592d28

Please all note that sharp is only working on "node": "^18.17.0 || ^20.3.0 || >=21.0.0", which means that its optional binary dependencies won’t be installed on other node versions. Node 16 is not an option.

https://github.com/lovell/sharp/blob/a77ac6ae259908a989d6c73e70f19d6994b7a237/package.json#L189

hi @gioboa, for my case I think it’s my mistake because not upgrading all qwik dependencies to the latest. I found the root cause when opening the issue in sharp repo (https://github.com/lovell/sharp/issues/3896).

So, I tried to upgrade to qwik latest wich is 1.3.0 and it’s solved the issue

@DeVoresyah you have an issue with Cloudflare and sharp, right?

npm i --package-lock=false worked for me.

package-lock Default: true Type: Boolean If set to false, then ignore package-lock.json files when installing. This will also prevent writing package-lock.json if save is true.

I’ve attempted to improve the messaging in sharp via commit https://github.com/lovell/sharp/commit/3a0c37569219d10321f559fc63715ef45aca0c0d but the summary here is that npm is currently the wrong choice of package manager if you want multi-platform support.

@Przemoo16 can you try adding

  "optionalDependencies": {
    "@img/sharp-linux-x64": "^0.33.0"    
  },

to your package.json?