How do I install npm packages on Google Cloud Functions?

不想你离开。 提交于 2020-01-16 14:56:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!