setup webpack and docker for sharp library

706 views Asked by At

I am trying to use sharp (node image processing library) with docker and webpack. Everything works fine with my local environment on my own machine but when I am using docker things are not working and I get an error Can't find module sharp

In webpack configuration, I marked sharp as external dependency

externals: {
    sharp: 'sharp',
}

I bundle my server in a file main.js then I copy the file to the docker container and finally install sharp on my docker container. Here is my Docker file

FROM node:12.18.1

USER node
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH=$PATH:/home/node/.npm-global/bin:/home/node/.npm-global/lib
 
RUN mkdir -p /home/node/server
WORKDIR /home/node/server


ENTRYPOINT ["node", "main.js"]
COPY dist/ .
RUN npm install --global  --verbose --arch=x64 --platform=linux sharp

I verified that sharp is actually installed in /home/node/.npm-global/lib so I am not sure why my bundle main.js can not find it?

1

There are 1 answers

0
Wael Showair On BEST ANSWER

It turns out this was just a node path problem. So I have to set NODE_PATH environment variable during runtime to /home/node/.npm-global/bin:/home/node/.npm-global/lib/node_modules. After doing that problem was solved and my bundle could locate sharp module