node.js - GCP 云功能:找不到 npm 模块
问题描述
所以我正在构建一个云功能,并将我的代码结构化为多个文件。
我的 index.js 是这样的:
const { postHandler } = require('./requestHandler.js');
const httpHandler = (req, res) => {
const { method } = req;
switch (method) {
case 'POST':
postHandler(req, res);
break;
case 'GET':
getHandler(req, res);
break;
case 'DELETE':
deleteHandler(req, res);
break;
case 'PUT':
putHandler(req, res);
break;
default:
return res.sendStatus(400).json({message: 'Invalid method. Allowed are POST, GET, DELETE'});
}
}
exports.httpHandler = httpHandler
这是 requestHandler
const createReviewApp = require('./createReviewApp')
const postHandler = ( req, res ) => {
const { path } = req;
return createReviewApp( req, res )
}
module.exports = { postHandler }
这是 createReviewApp
const _compute = require('./computeClass.js');
const _dns = require('./dnsClass.js');
const _storage = require('./storageClass.js');
const createReviewApp = ( req, res ) => {
// doing something
}
这是我的computeClass
const Compute = require('@google-cloud/compute')
const projectId = process.env.GOOGLE_PROJECT_ID;
class ComputeSingleton {
constructor(){
this.compute = new Compute({
projectId,
});
}
getVm({zoneName, vmName}){
return this.getZone({zoneName}).vm(vmName).get();
}
getZone({zoneName}){
return this.compute.zone(zoneName);
}
createVm({zoneName, vmName, config}){
return this.getZone({zoneName}).createVM(vmName, config);
}
deleteVm({zoneName, vmName}){
return this.getZone({zoneName}).vm(vmName).delete();
}
}
module.exports = new ComputeSingleton()
现在,在我的 computeClass 中,我收到以下错误:
Cannot find module '@google-cloud/compute
我已经安装了依赖项,是否需要将所有代码放在一个文件中?
编辑:我在功能 nodejs 模拟器上测试它
解决方案
不,您不需要将代码放在一个文件中。听起来你实际上并没有@google-cloud/compute
在你的函数项目 package.json 中完全安装。
推荐阅读
- functional-programming - 当我们谈论函数中的函数时,究竟什么是纯函数
- git - 适用于命令行和 Intellij 的 lint-staged + commitizen 配置
- amazon-ec2 - Amazon EC2:如何监控我的软件是否正在运行?
- go - 你如何处理 golang 中的 float64 比较?
- ruby-on-rails - 仅限登录用户的编辑和删除功能
- php - 作为函数调用者执行 return
- mysql - 如何安全地存储自由文本用户的答案
- flutter - 如何获取字符串中的所有项目名称
- dart - 为什么 meta.gps.gpsLatitude 为空?
- python-3.x - Python String - 需要在末尾打印带有双引号的字符串