linux-x64 binaries cannot be used on the linuxmusl-x64 platform error
Asked Answered
S

4

23

I'm installing the Sharp package for image compression on the docker image for a Nodejs project with package.json. When I create the container I get the following error regarding to the sharp package:

/app/node_modules/sharp/lib/libvips.js:67 
throw new Error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.`);
 ^ Error: 'linux-x64' binaries cannot be used on the 'linuxmusl-x64' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.
at Object.hasVendoredLibvips (/app/node_modules/sharp/lib/libvips.js:67:13)
at Object.<anonymous> (/app/node_modules/sharp/lib/constructor.js:8:22)
at Module._compile (module.js:577:32) 
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32) 
at tryModuleLoad (module.js:453:12) 
at Function.Module._load (module.js:445:3) 
at Module.require (module.js:504:17) 
at require (internal/module.js:20:19) 
at Object.<anonymous> (/app/node_modules/sharp/lib/index.js:3:15)'.

I have removed the whole node_module directory and reran the npm install command in the directory to reinstall all pack and also rebuilt the docker image but I'm still getting the error.

Any recommendations on how to fix this issue is appreciated.

Susansusana answered 14/9, 2019 at 4:49 Comment(7)
Can you paste in your console output instead of posting a screenshot? Thanks!Tattle
@GeneZ.Ragan I pasted it.Susansusana
Great. You can remove the image now!Tattle
How exactly are you building your image and running it? (For the standard Docker workflow I'm used to, you'd need to RUN npm install in your Dockerfile, make sure the node_modules directory is listed in a .dockerignore file, and you should not have any docker run -v options hiding or otherwise overwriting the code in the image.)Schlueter
@DavidMaze I'm using docker-compose to build image and run it, also in the Dockerfile, I used Run npm install command; I removed the image and rebuilt it, the sharp package installed successfully without any error but when I run the container I get the above error! I haven't .dockerignore file! Is it cause this error?Susansusana
Did you solve a problem @pichlou ?Maim
@Maim Yep, I used these links: sharp.pixelplumbing.com/en/latest/install/#aws-lambda and github.com/lovell/sharp/issues/1459Susansusana
E
41

I faced the same error with Docker. The problem was that I forgot to include a .dockerignore file and my node_modules were being copied into the container.

Try creating a .dockerignore file in the root of your project (next to your Dockerfile) with e.g.:

node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
Errantry answered 19/3, 2020 at 16:37 Comment(1)
This solved the issue for me as well. Thank you. Should be the accepted answer.Neilson
D
8

It's caused by the fact you run docker on a Linux platform and your machine is probably mac or windows. Most of the time you can use the same module versions but not when it uses low level kernel functions like sharp.

You need a different version of Sharp on Docker and on your local machine.

You have probably run your project without docker, then with docker.

Solution 1: You can remove package.lock + node_modules folder then rebuild and now only use docker.

Solution 2: (not clean but can help) Remove Sharp from you package.json and install it later when you start your server. For example by updating your package.json:

package.json

{
  ...
  "scripts": {
    ...
    "start-docker": "yarn add sharp && nodemon index.js"
  },
  ...

you can do it in your Dockerfile file as well:

Dockerfile

FROM node:13
ADD package.json /package.json
RUN yarn install
RUN yarn add sharp
ENV NODE_PATH=/node_modules
ENV PATH=$PATH:/node_modules/.bin
WORKDIR /app
CMD ["yarn","start-docker"]
Dispersant answered 3/2, 2020 at 11:0 Comment(1)
you probably meant package-lock.jsonUnderlaid
O
4

I faced the problem with multi-staged docker file where the two imagse are based on different platforms and I solved it like this:

FROM node:14 AS builder
WORKDIR /app
COPY ./package.json ./
RUN npm install
COPY . .
RUN npm run build

FROM node:14-alpine
WORKDIR /app
COPY --from=builder /app ./
RUN npm install sharp
CMD ["npm", "run", "start:prod"]

The trick is to install run npm install sharp in the final container - in my case it was Alpine linux that is different than the base image of node:14 (obviously it is different platform). Sharp is compiled directly to certain platform so running npm install in one container and copy those compiled / precompiled files to another container cannot work. I assume this is still better solution then fallback to node:14 image (only run container) that is way bigger (in my case 1,4Gb -> 0.7Gb Alpine).

Keep in mind that you should still have .dockeringore file with node_modules won't help you to solve this issue. It just speeds up process building with cache on CI server or on localhost (with different OS).

Cheers

Oringa answered 26/3, 2021 at 6:52 Comment(0)
M
0

Had the same issue with circle CI configuration. I was installing the node_modules on

docker: - image: cimg/node:16.14.0

and was running my build job on

docker: - image: node:16-alpine

And because of that, I had such an error

/root/project/node_modules/favicons-webpack-plugin/src/index.js:562
    throw new Error(
          ^
Error: Could not find the npm peerDependency "favicons".
Please run:
npm i favicons
 - or -
yarn add favicons

Error: 'linux-x64' binaries cannot be used on the 'linuxmusl-x64' platform. Please remove the 'node_modules/sharp' directory and run 'npm install' on the 'linuxmusl-x64' platform.
    at loadFaviconsLibrary (/root/project/node_modules/favicons-webpack-plugin/src/index.js:562:11)
    at FaviconsWebpackPlugin.generateFaviconsWebapp (/root/project/node_modules/favicons-webpack-plugin/src/index.js:403:22)
    at FaviconsWebpackPlugin.generateFavicons (/root/project/node_modules/favicons-webpack-plugin/src/index.js:315:21)
    at /root/project/node_modules/favicons-webpack-plugin/src/index.js:117:18
    at /root/project/node_modules/favicons-webpack-plugin/src/cache.js:155:5
    at CacheFacade.providePromise (/root/project/node_modules/webpack/lib/CacheFacade.js:337:24)

Exited with code exit status 1
CircleCI received exit code 1

The solution is to run all these jobs on the same docker image

Morale answered 15/6, 2022 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.