I am creating a google function. However, when I try to deploy to Google Cloud Platform, I am getting this error
我正在创建一个谷歌函数。然而,当我尝试部署到谷歌云平台时,我得到了这个错误
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function load error: Code in file index.js can't be loaded. Did you list all required modules in the package.json dependencies? Detailed stack trace: Error: Cannot find module 'request'
操作错误:代码=3,消息=Function load ERROR:文件索引中的代码。js不能被加载。你把包里所有需要的模块都列出来了吗?json依赖性?详细堆栈跟踪:错误:找不到模块“请求”
How do I upload/install the 'request' library in google cloud platform?
如何在谷歌云平台上上传/安装“请求”库?
Code Snippet
'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);
});
});
}
-Alan-
出,
1 个解决方案
#1
3
Read through the Google Cloud documentation regarding dependencies: https://cloud.google.com/functions/docs/writing/dependencies
阅读关于依赖项的谷歌云文档:https://cloud.google.com/functions/docs/writing/dependencies。
List the 'request' module as a dependency in your package.json file if using the gcloud CLI.
将“请求”模块列为包中的一个依赖项。使用gcloud CLI的json文件。
Or, run 'npm install --save request' in the folder containing your cloud function and upload your pre-installed dependencies as part of your ZIP file.
或者,运行“npm安装——保存在包含您的云函数的文件夹中的请求”,并将预安装的依赖项上载到您的ZIP文件中。
#1
3
Read through the Google Cloud documentation regarding dependencies: https://cloud.google.com/functions/docs/writing/dependencies
阅读关于依赖项的谷歌云文档:https://cloud.google.com/functions/docs/writing/dependencies。
List the 'request' module as a dependency in your package.json file if using the gcloud CLI.
将“请求”模块列为包中的一个依赖项。使用gcloud CLI的json文件。
Or, run 'npm install --save request' in the folder containing your cloud function and upload your pre-installed dependencies as part of your ZIP file.
或者,运行“npm安装——保存在包含您的云函数的文件夹中的请求”,并将预安装的依赖项上载到您的ZIP文件中。