我正在创建一个google功能。然而,当我尝试部署到Google平台时,我会得到这个错误
错误:(gcloud.beta.functions.deploy) OperationError: code=3,message=Function加载错误:无法加载index.js文件中的代码。在package.json依赖项中列出了所有必需的模块吗?详细堆栈跟踪:错误:找不到模块‘请求’
如何在google云平台上上传/安装“请求”库?
代码段
'use strict';
const https = require('https');
const host = 'https://www.example.com';
const clientId = 'qpopMIGtVdeIdVk3oEtr2LGbn8vTeTWz';
const clientSecret = 'eUnsWQ8y3AuiFHJu';
const grant_type = 'client_credentials';
const resource = 'b.microsoft.com/4fa4b4a7-d34f-49af-8781-c8b39f0cf770';
const request = require("request");
exports.oauthtoken = (req, res) => {
// Call the Apigee API
callGetOAuthToken().then((output) => {
// Return the results from the APigee to DialogFlow
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
}).catch((error) => {
// If there is an error let the user know
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
});
};
function callGetOAuthToken () {
return new Promise((resolve, reject) => {
let path = '/customers/v1/accesstoken';
var authHeader = Buffer.from(clientId + ':' + clientSecret).toString('base64');
var post_options = {
url: host + path,
method: 'POST',
headers:
{
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + authHeader,
'grant_type':grant_type
}
};
// Make the HTTP request to get the weather
request(post_options, function(err, res, body) {
let output = JSON.parse(body);
console.log(output);
resolve(output);
});
});
}-艾伦-
发布于 2018-04-17 06:15:19
阅读Google关于依赖关系的文档:https://cloud.google.com/functions/docs/writing/dependencies
如果使用gcloud,请将“请求”模块作为package.json文件中的依赖项列出。
或者,在包含云功能的文件夹中运行“npm安装-保存请求”,并将预安装的依赖项上传为ZIP文件的一部分。
https://stackoverflow.com/questions/49870892
复制相似问题