6

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);
        })
    }
}
4
  • did you try this npm i --save firebase-functions@latest? Commented Feb 7, 2018 at 7:56
  • Please show the minimal code from your index.js that reproduces this error. Commented Feb 7, 2018 at 7:58
  • @PeterHaddad, yes I did. I get back the first error about express. Kind of, stuck in a cycle. Commented Feb 7, 2018 at 7:58
  • FYI, just checked the node_modules folder, express exists. Commented Feb 7, 2018 at 8:36

3 Answers 3

8

After breaking my head for a day, I did the obvious. I installed Express and did a deploy but this time I got the same error for body-parser, _Error: Cannot find module 'body-parser'_ Installed body-parser as well. Deployed again, voila!! deployment was successful.

But I'm not really sure why I had to explicitly install them even though both express and body-parser module were present in the node-modules folder.

Summary:

npm install --save --save-exact [email protected]    
npm install --save --save-exact [email protected]    
firebase deploy --only functions  
Sign up to request clarification or add additional context in comments.

1 Comment

In my case it was that I put it not into functions folder, but root of the firebase. I think the one reading this will get it.
3

first use

npm install --save firebase

in C:\Users\HP\Desktop\New folder (2)\functions>

remember install firebase should be in functions folder the it will work properly

you should see this dependencies

"dependencies": { "express": "4.17.1", "firebase": "^7.14.3", "firebase-admin": "^8.10.0", "firebase-functions": "^3.6.1" },

Comments

1

The reason you see express in the node_modules folder but still get the error is because it is not added in the package.json. Add it in the package.json in dependencies like this:

"dependencies":{
   "express": "4.16.3",
   "firebase-admin": "5.13.0",
   "firebase-functions": "2.0.2"
 }

You should be able to deploy now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.