'Google Cloud Functions error: "Cannot find module 'sharp'" but it's in my package.json

I am trying to deploy a function to Google Cloud Functions. I based it on their ImageMagick tutorial.

Every time, the function fails to deploy because it reaches an error. Looking at the log, the error is:

    Provided module can't be loaded. 
    Did you list all required modules in the package.json dependencies? 
    Detailed stack trace: 
    Error: Cannot find module 'sharp' 

I can't figure out why this is happening, because sharp is in my package.json dependencies. If I open the web editor for the function in the Google Cloud console, the package.json is there as one of the files and shows sharp as a dependency. I tried running npm install and npm install --save and re-deploying, and that hasn't fixed anything.

I'm including the package in the function with const sharp = require('sharp'); (this is the line where the log shows the error occurring), and this is my package.json:

{
  "name": "Resize images",
  "version": "0.0.1",
  "private": true,
  "author": "James Tyner",
  "engines": {
    "node": ">=10.0.0"
  },
  "dependencies": {
    "@google-cloud/storage": "^5.0.0",
    "sharp": "^0.25.4"
  }
}

Can you help me figure out what I'm doing wrong?



Solution 1:[1]

Somehow I was able to address the issue, but I don't fully understand what I did differently. I found that the dependencies listed in package.json weren't being installed when I ran npm install, so I created a separate folder and copied my code there, ran npm install in the new folder, and it worked well from there. Since then, the dependencies have been working properly when I change them and re-deploy the function.

Solution 2:[2]

Using Node v12.13.1 and serverless deployment with webpack to GCP and cloud-functions, I've struggled with this issue. In my case it was a different module though. The problem is that no module from node_modules will be possible to require or import. The reason becomes clear if one takes a look at the webpack zip-file in directory .serverless. It seems that with GCP nothing but the file (typically index.js) denoted as "main" in package.json is actually included.

The solution was to adapt webpack.config.js to explicitly include those files missing.

webpack.config.js

Solution 3:[3]

This has happened to me many times since I was tricked to install packages in the project directory. It works fine locally but creates an error when you try to deploy.

It worked for me when I changed directory into the functions folder, instead of the firebase project folder and did a package install in there

cd functions
npm install [your missing package] --save

Solution 4:[4]

I was running into this issue. Various dependencies were causing my function deployment to fail. After a bit of digging I found that the peer-dependencies were not being included.

Adding this fixed my issue

"scripts": {
   ...
  "gcp-build": "npm i npm-install-peers"
},

checking the docs. the gcp-build command allows us to perform a custom build step during the function build process.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Tyner
Solution 2
Solution 3 Abraham
Solution 4 Thomas Valadez