DEV Community

loading...
Cover image for Deploy to firebase functions

Deploy to firebase functions

Stanley Gomes
Developer passionate about technology.
Updated on ・1 min read

Today we'll learn how to deploy you node app to firebase functions. You can also use nodevader boilerplate to do this tutorial.

First, install the Firebase CLI if you don't have it.

npm install -g firebase-tools   
Enter fullscreen mode Exit fullscreen mode

Then, run, login to your firebase account and init a project, right after choose option functions and connect it to your project

firebase login  
firebase init   
Enter fullscreen mode Exit fullscreen mode

Now, you have a folder called functions.

Yes, delete it.

rm -rf functions    
Enter fullscreen mode Exit fullscreen mode

Go to the firebase.json file and add the source property to functions, pointing to current directory:

{   
  "functions": {    
    "source": ".",  
    ... 
  }
}
Enter fullscreen mode Exit fullscreen mode

Now install dependencies for firebase

npm install --save firebase-admin   
npm install --save firebase-functions   
npm install --save-dev firebase-functions-test  
Enter fullscreen mode Exit fullscreen mode

and, add the required engine property to your package.json file

  { 
    ... 
    "engines": {    
      "node": "8"   
    }   
  } 
Enter fullscreen mode Exit fullscreen mode

We're almost there. Go to your src/index.js file, remove app.listen(...)

// app.listen(...)  
Enter fullscreen mode Exit fullscreen mode

And the last step. Add functions dependency and export the function.

const functions = require('firebase-functions') 
// your code here ...   
const api = functions.https.onRequest(app)  
module.exports = {  
  api   
}   
Enter fullscreen mode Exit fullscreen mode

Now you can deploy your function to firebase

firebase deploy 
Enter fullscreen mode Exit fullscreen mode

If you want to get the firebase CLI key for CI/CD. Run

firebase login:ci   
Enter fullscreen mode Exit fullscreen mode

Thanks and Good luck.

Discussion (0)