[英]GCP Cloud Functions "Cannot find module 'express'" when it IS in my package.json
[英]Google Cloud Functions error: "Cannot find module 'sharp'" but it's in my package.json
我正在尝试将 function 部署到 Google Cloud Functions。 我基于他们的 ImageMagick 教程。
每次 function 都部署失败,因为它到达错误。 查看日志,报错如下:
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'
我不明白为什么会这样,因为sharp
在我的package.json
依赖项中。 如果我在 Google Cloud 控制台中打开 function 的sharp
编辑器,则package.json
作为文件之一存在,并显示为依赖项。 我尝试运行npm install
和npm install --save
并重新部署,但没有解决任何问题。
我将 package 包含在 function 中,其中包含const sharp = require('sharp');
(这是日志显示发生错误的行),这是我的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"
}
}
你能帮我弄清楚我做错了什么吗?
我遇到了这个问题。 各种依赖项导致我的 function 部署失败。 经过一番挖掘后,我发现没有包含对等依赖项。
添加这个解决了我的问题
"scripts": {
...
"gcp-build": "npm i npm-install-peers"
},
检查文档。 gcp-build
命令允许我们在 function 构建过程中执行自定义构建步骤。
不知何故,我能够解决这个问题,但我不完全理解我做了什么不同的事情。 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. 从那时起,当我更改它们并重新部署 function 时,依赖关系一直正常工作。
使用 Node v12.13.1 和 webpack 到 GCP 和云功能的无服务器部署,我一直在努力解决这个问题。 就我而言,它是一个不同的模块。 问题是 node_modules 中的任何模块都不可能需要或导入。 如果查看 directory.serverless 中的 webpack zip 文件,原因就很清楚了。 似乎除了在 package.json 中表示为“main”的文件(通常是 index.js)之外,什么都没有。
解决方案是调整 webpack.config.js 以明确包含那些丢失的文件。
自从我被骗在项目目录中安装软件包以来,这种情况发生在我身上很多次。 它在本地运行良好,但在您尝试部署时会产生错误。
当我将目录更改为functions
文件夹而不是 firebase 项目文件夹并在那里安装 package 时,它对我有用
cd functions
npm install [your missing package] --save
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.