如何在Google Cloud Functions上安装npm软件包?
我正在尝试创建一个简单的函数,该函数:
I'm trying to create a simple function that:
- 从公共网址获取JSON文件
- 进行一些运算,然后给出答案.
我认为Google Cloud Functions非常适合,因为我可以用JS编写代码,而不必担心服务器部署等问题.
I figured that Google Cloud Functions would be the perfect fit since I can code in JS and don`t have to worry about server deployment, etc.
我从没真正使用过nodejs
/npm
,所以也许是问题所在,但是我尝试在线阅读,他们只是提到
I've never really used nodejs
/npm
so maybe this is the issue, but I tried reading online and they just mention
npm install package-name
我不确定在"Google云功能"页面上的哪里可以执行此操作.
I'm not sure where I can do this on the Google Cloud Functions page.
我当前正在使用嵌入式编辑器,并且具有以下内容:
I'm currently using the inline editor and I have the following:
var fetch = require('node-fetch');
exports.test2 = (req, res) => {
fetch("myURLgoes here").then(function(response){
res.status(200).send('Success:'+response);
});
我收到以下错误:
错误:函数崩溃了.详细信息: 提取未定义
Error: function crashed.Details: fetch is not defined
从Google Cloud Platform控制台转到您的云功能.
From Google Cloud Platform console, go to your cloud functions.
创建或编辑函数时,应该有两个文件: -index.js:您在其中定义函数的位置 -package.json:您在其中定义依赖项的地方.
You should have two files when creating or editing a functions: - index.js: where you define your functions - package.json: where you define your dependency.
开始时您的package.json是这样的:
Your package.json at the start is something like this:
{
"name": "sample-http",
"version": "0.0.1"
}
使用以下命令npm install在package.json中添加所有要安装的模块:
Add in your package.json all your module that you'd like to install with the command npm install as below:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@material-ui/core": "^4.1.1"
}
}
您可以在www.npmjs.com上找到该软件包的最新版本
you can find the last version of the package on www.npmjs.com