简体   繁体   English

Google Cloud Functions 错误:“找不到模块‘sharp’”,但它在我的 package.json 中

[英]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.我正在尝试将 function 部署到 Google Cloud Functions。 I based it on their ImageMagick tutorial .我基于他们的 ImageMagick 教程

Every time, the function fails to deploy because it reaches an error.每次 function 都部署失败,因为它到达错误。 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.我不明白为什么会这样,因为sharp我的package.json依赖项中。 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.如果我在 Google Cloud 控制台中打开 function 的sharp编辑器,则package.json作为文件之一存在,并显示为依赖项。 I tried running npm install and npm install --save and re-deploying, and that hasn't fixed anything.我尝试运行npm installnpm install --save并重新部署,但没有解决任何问题。

I'm including the package in the function with const sharp = require('sharp');我将 package 包含在 function 中,其中包含const sharp = require('sharp'); (this is the line where the log shows the error occurring), and this is my package.json : (这是日志显示发生错误的行),这是我的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?你能帮我弄清楚我做错了什么吗?

I was running into this issue.我遇到了这个问题。 Various dependencies were causing my function deployment to fail.各种依赖项导致我的 function 部署失败。 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. gcp-build命令允许我们在 function 构建过程中执行自定义构建步骤。

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. 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.从那时起,当我更改它们并重新部署 function 时,依赖关系一直正常工作。

Using Node v12.13.1 and serverless deployment with webpack to GCP and cloud-functions, I've struggled with this issue.使用 Node v12.13.1 和 webpack 到 GCP 和云功能的无服务器部署,我一直在努力解决这个问题。 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.问题是 node_modules 中的任何模块都不可能需要或导入。 The reason becomes clear if one takes a look at the webpack zip-file in directory.serverless.如果查看 directory.serverless 中的 webpack zip 文件,原因就很清楚了。 It seems that with GCP nothing but the file (typically index.js) denoted as "main" in package.json is actually included.似乎除了在 package.json 中表示为“main”的文件(通常是 index.js)之外,什么都没有。

The solution was to adapt webpack.config.js to explicitly include those files missing.解决方案是调整 webpack.config.js 以明确包含那些丢失的文件。

webpack.config.js webpack.config.js

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当我将目录更改为functions文件夹而不是 firebase 项目文件夹并在那里安装 package 时,它对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCP Cloud Functions 在我的 package.json 中“找不到模块‘express’” - GCP Cloud Functions "Cannot find module 'express'" when it IS in my package.json 错误:找不到模块“../../package.json” - Error: Cannot find module '../../package.json' Openshift部署错误:找不到模块package.json - Openshift deploy error: Cannot find module package.json 错误:即使在 package.json 中列出,也找不到模块“ejs” - Error: Cannot find module 'ejs' even though it's listed in package.json 电子找不到模块 package.json - electron cannot find module package.json 为什么 Heroku 告诉我它在我的模块中找不到 package.json 当我执行 Z3115FB307DFCB5BA733BZ76 时 - Why is Heroku telling me it cannot find a package.json in my module when I do a heroku push Electron 带 Strapi,找不到模块 package.json - Electron with strapi, Cannot find module package.json 错误:找不到模块'@angular-devkit/build-angular/package.json' - Error: Cannot find module '@angular-devkit/build-angular/package.json' 错误:找不到模块 'node-linux-x64/package.json' - 第一次推送到 Heroku - Error: Cannot find module 'node-linux-x64/package.json' - pushing to Heroku for first time 推送到Heroku时出错:找不到模块“ node-linux-x64 / package.json” - Error while pushing to Heroku: Cannot find module 'node-linux-x64/package.json'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM