FIrebase deploy error: Cannot find module 'firebase-admin'
Asked Answered
H

9

33

I recently started getting the error below when trying to deploy to Firebase (after having done so successfully in the past). I'm not sure what might have changed for this to start occurring. If I run firebase serve to serve on the localhost, everything works fine. My package.json and requires from index.js are also below.

i  deploying functions, hosting
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
+  runtimeconfig: all necessary APIs are enabled
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (46.02 KB) for uploading
+  functions: functions folder uploaded successfully
i  hosting: preparing public directory for upload...
!  Warning: Public directory does not contain index.html
+  hosting: 9 files uploaded successfully
i  starting release process (may take several minutes)...
i  functions: updating function app...
!  functions[app]: Deploy 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 'firebase-admin'
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.<anonymous> (/user_code/node_modules/firebase-functions/lib/apps.j
s:25:16)
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)


Functions deploy had errors. To continue deploying other features (such as datab
ase), run:
firebase deploy --except functions

Error: Functions did not deploy properly.

package.json:

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"@google-cloud/vision": "^0.12.0",
"async": "^2.5.0",
"consolidate": "^0.14.5",
"express": "^4.15.4",
"firebase-admin": "^5.4.0",
"firebase-functions": "^0.5.7",
"handlebars": "^4.0.10",
"jquery": "^3.2.1",
"js-levenshtein": "^1.1.3",
"json-query": "^2.2.2"
},
"private": true
}

From index.js:

const functions = require('firebase-functions');
const firebase = require('firebase-admin');
const express = require('express');
const engines = require('consolidate');
const Vision = require('@google-cloud/vision');
const levenshtein = require('js-levenshtein');
const restName = require('./restName');
const parser = require('./parser');
const jsonQuery = require('json-query')

const firebaseApp = firebase.initializeApp(
functions.config().firebase
);
Houseman answered 19/10, 2017 at 4:21 Comment(1)
Just started getting the same errorDeclarant
L
28

They have an active service disruption. Follow this for a workaround: https://status.firebase.google.com/incident/Functions/17024

Run the following commands inside the functions repository:

npm install --save-exact [email protected] npm install --save-exact [email protected]

Then try deploying functions again:

firebase deploy --only functions

If npm doesn't work, you can try yarn which worked for us:

yarn add [email protected] --exact yarn add [email protected] --exact
Lamellar answered 19/10, 2017 at 4:41 Comment(4)
It works! Thanks! And double thanks for the yarn hint! npm wouldn't install it due to "Unsupported engine for [email protected]: wanted: {"node":"~0.10.12"} (current: {"node":"6.11.1","npm":"5.5.1"})"Argumentative
seems to be fixed on my side (Norway)... neither the npm nor the yarn workaround worked for me.Declarant
I'm now getting: Deploy Error: Failed to configure trigger providers/cloud.firestore/eventTypes/[email protected]Geostatics
Upgrading from beta to v1 fixed this for me: firebase.google.com/docs/functions/beta-v1-diffMcgowen
D
7

Update file package.json in folder functions and look at dependencies remove sign ~ from firebase-admin and ^ from firebase-functions it should be like:

"dependencies": {
  "firebase-admin": "5.4.0",
  "firebase-functions": "0.7.0"
}

in command line type

npm install

and then try to deploy again.

Doy answered 19/10, 2017 at 6:43 Comment(0)
D
6

run the following command

npm i firebase-admin 
Daladier answered 31/3, 2021 at 21:9 Comment(0)
A
4

enter image description here

So the issue is that package.json inside Functions folder is different from package.json in your project directory. yes, you have 2 of this file!

So if you want to deploy, (deploy Functions), run npm install express or install other dependencies inside Function folder and you will be able to deploy with no error.

Ambala answered 27/8, 2018 at 12:25 Comment(0)
M
3

I went to the functions directory and ran yarn (or npm install if you are old-school). Then went back to my project directory and ran firebase deploy again.

Manas answered 5/1, 2018 at 0:6 Comment(0)
L
1

The things I did to fix this

1.) Make sure I was in the proper directory's package.json

2.) rm -rf node-modules

3.) npm i (missing module)

4.) Made the missing dependencies were NOT in devDependencies but rather in dependencies in the package.json, nothing in devDependencies gets deployed

Lamellar answered 3/5, 2022 at 5:47 Comment(0)
S
0

I also got same error. i fix that error from installing different version in nodejs. I uninstall my new version and i install Node v7.8.0. then it works fine :)

Selfindulgent answered 5/12, 2017 at 11:39 Comment(0)
C
0

Try installing: npm i -g [email protected]

This solved my issue.

Cameraman answered 9/5, 2019 at 13:41 Comment(0)
O
0

If using docker, ensure your local version of nodejs is the same as the one you are using on the docker file. For example, if I am using node v16.14.0, (find out by typing this in your terminal) node -v, then sample docker file should be :

FROM node:16.14.0-alpine                             
ENV NODE_ENV=production                 
WORKDIR /app                            
COPY package*.json ./                  
RUN npm install --production            
COPY . .                                
CMD [ "npm", "start" ] 

             
Obscuration answered 29/3, 2022 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.