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

4

5

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?

Farica answered 8/8, 2020 at 6:27 Comment(2)
Can you share the code of your function?, I suspect that this issue is caused because Cloud functions doesn't have the OS package libvips, this is a requirement for sharp as is mentioned on the npm pageImpartible
@J.A.Hernández Thanks for following up — 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.Farica
F
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.

Farica answered 16/8, 2020 at 0:20 Comment(0)
D
7

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
Darra answered 10/7, 2021 at 0:33 Comment(1)
Also works as advertised for @google-cloud/visionPlumose
B
2

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.

Bondholder answered 7/5, 2022 at 3:58 Comment(0)
F
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.

Farica answered 16/8, 2020 at 0:20 Comment(0)
R
1

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

Rem answered 30/9, 2020 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.