Alexandre Paiva
Alexandre Paiva

Reputation: 1553

Could not load the "sharp" module using the win32-x64 runtime (Strapi)

I used to use a Node.js v16.x installed locally to run Strapi which worked well.

But recently I had to update it to Node.js v18.x due to another project, so I installed NVM to manage multiple Node.js versions on my PC - and now I want to use Strapi with the new Node.js but I can't even start the develop mode anymore. There's a problem with the package Sharp.

I'm not sure whether the issue with the package Sharp started because of the new Node version or NVM. As the Node installed (v18.17.0) is listed as compatible on Sharp package npm page (here), I guess the problem is with NVM.

nvm list

  * 18.17.0 (Currently using 64-bit executable)
    16.14.2

My dependencies:

  "dependencies": {
    "@strapi/plugin-i18n": "4.16.2",
    "@strapi/plugin-users-permissions": "4.16.2",
    "@strapi/provider-email-amazon-ses": "4.16.2",
    "@strapi/provider-upload-aws-s3": "4.16.2",
    "@strapi/strapi": "4.16.2",
    "@strapi/utils": "4.16.2",
    "pg": "8.11.3",
    "reach": "1.0.1",
    "react": "^18.0.0",
    "react-dom": "^18.0.0",
    "react-router-dom": "5.2.0",
    "sharp": "^0.33.1",
    "slugify": "1.6.6",
    "strapi-plugin-config-sync": "1.2.3",
    "strapi-plugin-email-designer": "2.2.1",
    "strapi-plugin-entity-relationship-chart": "4.14.6",
    "strapi-plugin-import-export-entries": "1.23.1",
    "strapi-plugin-notes": "1.1.2",
    "strapi-plugin-placeholder": "4.4.0",
    "strapi-plugin-publisher": "1.5.6",
    "styled-components": "5.2.1"
  },
  "engines": {
    "node": ">=14.x.x <=18.x.x",
    "npm": ">=6.0.0"
  }

I've already tried to run the commands suggested on log (attached below) but nothing worked.

Does anyone know how to fix it?

enter image description here

Upvotes: 16

Views: 25485

Answers (11)

Mel K.
Mel K.

Reputation: 317

In my case, I'm working with pnpm and had to add the following to my package.json

 "pnpm": {
    ...,
    "onlyBuiltDependencies": [
      "sharp"
    ]
  }

Upvotes: 0

Anatoliy Vlasyuk
Anatoliy Vlasyuk

Reputation: 1

Try forcing all dependencies to use the same version of sharp. To do this, add "resolutions" to the root package.json:

"resolutions": {
  "sharp": "^0.33.5"
}

then follow:

yarn install

Upvotes: 0

Muhammad Tameem Rafay
Muhammad Tameem Rafay

Reputation: 4585

This command works for me

npm install --arch=x64 --platform=linux --target=18 sharp 

Upvotes: 0

Rama
Rama

Reputation: 11

npm install sharp

npm install --force @img/sharp-win32-x64

Upvotes: 1

CenterADiv
CenterADiv

Reputation: 81

Answer from @M. Nicol was helpful.

npm install --include=optional sharp

For me it made also sense to install the optional packages globally, since i dont need the dev dependeny in my package.json

npm install -g --include=optional sharp

Upvotes: 1

Serafim
Serafim

Reputation: 483

It works for me: yarn add sharp --ignore-engines

p.s. it tested on win10 x64.

Upvotes: 0

Ashraful Sm
Ashraful Sm

Reputation: 1

The problem is your npm package is not updated. When your package isn't matching with the sharp package. Easily Solving way is update your npm.

npm install -g npm@latest

if not working Clear the cache

npm cache clean -f

Then install the dependencies

npm i sharp

Hope working.

Upvotes: 0

M. Nicol
M. Nicol

Reputation: 133

Make sure optional dependencies are installed:

npm install --include=optional sharp

Upvotes: 7

Kazi Mahbubur Rahman
Kazi Mahbubur Rahman

Reputation: 155

  1. Update your package manager

npm install -g npm@latest

  1. Clear the cache

npm cache clean -f

  1. Then Terminate batch job and run again

npm run dev

or

npm run start

Upvotes: 3

Nico Serrano
Nico Serrano

Reputation: 797

The following answer is related to the sharp error: Could not load the "sharp" module using the win32-x64 runtime and not specifically with Strapi.

It looks like sharp needs some builds to run in Windows and in /node_modules/sharp/lib/sharp.js (line 13) you can notice that they listed a couple of paths:

const paths = [
  `../src/build/Release/sharp-${runtimePlatform}.node`,
  '../src/build/Release/sharp-wasm32.node',
  `@img/sharp-${runtimePlatform}/sharp.node`,
  '@img/sharp-wasm32/sharp.node'
];
// as of February 17, 2024

To fix this error in Windows (using PowerShell terminal and not WSL) I installed @img/sharp-win32-x64 with npm i @img/sharp-win32-x64 --save-optional (notice the --save-optional flag) so when I run the same project in a Linux environment, npm will ignore @img/sharp-win32-x64 and safely continue.

This way in my particular case I am able to run my NodeJS project + sharp in Windows and Linux.

Upvotes: 3

Alexandre Paiva
Alexandre Paiva

Reputation: 1553

I was not able to fix it, not sure if it's something wrong with the sharp latest version 0.33.1 (that I had updated on my project).

I even tried to run the Strapi with Docker on a Linux image (node:18-alpine) but that issue still happen:

Error: Could not load js config file
/opt/node_modules/strapi-plugin-placeholder/strapi-server.js: Could not load the "sharp" module using the linuxmusl-x64 runtime.

Possible solutions:
- Ensure optional dependencies can be installed:
  npm install --include=optional sharp
  yarn add sharp --ignore-engines
- Ensure your package manager supports multi-platform installation:
  See https://sharp.pixelplumbing.com/install#cross-platform
- Add platform-specific dependencies:
  npm install --os=linuxmusl --cpu=x64 sharp
  npm install --force @img/sharp-linuxmusl-x64
- Consult the installation documentation:
  See https://sharp.pixelplumbing.com/install

at loadJsFile (/opt/node_modules/@strapi/strapi/dist/core/app-configuration/load-config-file.js:18:13)
at Module.loadFile (/opt/node_modules/@strapi/strapi/dist/core/app-configuration/load-config-file.js:37:14)
at Object.loadPlugins (/opt/node_modules/@strapi/strapi/dist/core/loaders/plugins/index.js:90:41)
at async Strapi.loadPlugins (/opt/node_modules/@strapi/strapi/dist/Strapi.js:310:5)
at async Promise.all (index 3)
at async Strapi.register (/opt/node_modules/@strapi/strapi/dist/Strapi.js:340:5)
at async Strapi.load (/opt/node_modules/@strapi/strapi/dist/Strapi.js:424:5)
at async Object.develop (/opt/node_modules/@strapi/admin/dist/_chunks/index-ffd2f664.js:1245:28)
at async develop (/opt/node_modules/@strapi/admin/dist/_chunks/develop-da585b1e.js:52:5)
at async Command.parseAsync (/opt/node_modules/commander/lib/command.js:923:5)
at async Module.runStrapiCommand (/opt/node_modules/@strapi/strapi/dist/commands/index.js:124:3)

The only solution was to downgrade to the previous version I was using (0.32.6), then it's working again.

Upvotes: 15

Related Questions