node.jsnpmgoogle-cloud-functionssharp

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

  • 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.