问题
I'm trying to create a simple function that:
- fetches a JSON file from a public URL
- does a little number crunching and spits out an answer.
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.
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
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);
});
I get the following error:
Error: function crashed.Details: fetch is not defined
回答1:
From Google Cloud Platform console, go to your cloud functions.
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.
Your package.json at the start is something like this:
{
"name": "sample-http",
"version": "0.0.1"
}
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"
}
}
you can find the last version of the package on www.npmjs.com
来源:https://stackoverflow.com/questions/48428733/how-do-i-install-npm-packages-on-google-cloud-functions