I am trying to deploy my functions to firebase, I'm getting the following error:
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 'express'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/user_code/index.js:6:15)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Firebase:
"firebase-admin": "~5.8.1"
"firebase-functions": "^0.8.1"
This issue is already raised here : https://github.com/firebase/firebase-tools/issues/515 (But for 'firebase-admin' module)
Based on inputs from there, I tried doing this: npm install --save-exact [email protected] npm install --save-exact [email protected]. but I ran into the following error :
Error: Error occurred while parsing your function triggers. Please ensure you have the latest firebase-functions SDK by running "npm i --save firebase-functions@latest" inside your functions folder.
Node version, v8.4.0.
Index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var express = require('express');
var bodyParser = require('body-parser');
const users = require('../functions/controllers/users.controller');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
app.post('/users', users.createUsers);
exports.myappusers = functions.https.onRequest(app);
users.controller.js
const admin = require('firebase-admin');
const db = admin.firestore();
exports.createUsers = function(req, res){
if(!req.body.name){
return res.status(404).send({message: 'User name cannot be empty'})
}
else{
db.collection('user-test')
.add({
name: req.body.name
})
.then((writeResult) => {
return res.json({ result: `User ID: ${writeResult.id} added.`});
})
.catch((err)=>{
return res.status(500).send(err);
})
}
}
npm i --save firebase-functions@latest?