Google App Engine - Node: Cannot find module 'firebase-admin'

ypicard picture ypicard · Feb 11, 2017 · Viewed 11.3k times · Source

First time deploying a GAE app here, I'm trying to follow this tutorial: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html (see node code section).

I ran

npm install firebase-admin --save
npm install request --save

on my machine, the package.json is here, but when I use gcloud app deploy, I get these logs when opening the app on my browser:

2017-02-10 09:35:02 default[20170210t103151]  module.js:471
2017-02-10 09:35:02 default[20170210t103151]      throw err;
2017-02-10 09:35:02 default[20170210t103151]      ^
2017-02-10 09:35:02 default[20170210t103151]
2017-02-10 09:35:02 default[20170210t103151]  Error: Cannot find module 'firebase-admin'
2017-02-10 09:35:02 default[20170210t103151]      at Function.Module._resolveFilename (module.js:469:15)
2017-02-10 09:35:02 default[20170210t103151]      at Function.Module._load (module.js:417:25)
2017-02-10 09:35:02 default[20170210t103151]      at Module.require (module.js:497:17)
2017-02-10 09:35:02 default[20170210t103151]      at require (internal/module.js:20:19)
2017-02-10 09:35:02 default[20170210t103151]      at Object.<anonymous> (/app/server.js:1:78)
2017-02-10 09:35:02 default[20170210t103151]      at Module._compile (module.js:570:32)
2017-02-10 09:35:02 default[20170210t103151]      at Object.Module._extensions..js (module.js:579:10)
2017-02-10 09:35:02 default[20170210t103151]      at Module.load (module.js:487:32)
2017-02-10 09:35:02 default[20170210t103151]      at tryModuleLoad (module.js:446:12)
2017-02-10 09:35:02 default[20170210t103151]      at Function.Module._load (module.js:438:3)
2017-02-10 09:35:07 default[20170210t103151]  module.js:471
2017-02-10 09:35:07 default[20170210t103151]      throw err;
2017-02-10 09:35:07 default[20170210t103151]      ^
2017-02-10 09:35:07 default[20170210t103151]
2017-02-10 09:35:07 default[20170210t103151]  Error: Cannot find module 'firebase-admin'
2017-02-10 09:35:07 default[20170210t103151]      at Function.Module._resolveFilename (module.js:469:15)
2017-02-10 09:35:07 default[20170210t103151]      at Function.Module._load (module.js:417:25)
2017-02-10 09:35:07 default[20170210t103151]      at Module.require (module.js:497:17)
2017-02-10 09:35:07 default[20170210t103151]      at require (internal/module.js:20:19)
2017-02-10 09:35:07 default[20170210t103151]      at Object.<anonymous> (/app/server.js:1:78)
2017-02-10 09:35:07 default[20170210t103151]      at Module._compile (module.js:570:32)
2017-02-10 09:35:07 default[20170210t103151]      at Object.Module._extensions..js (module.js:579:10)
2017-02-10 09:35:07 default[20170210t103151]      at Module.load (module.js:487:32)
2017-02-10 09:35:07 default[20170210t103151]      at tryModuleLoad (module.js:446:12)
2017-02-10 09:35:07 default[20170210t103151]      at Function.Module._load (module.js:438:3)
2017-02-10 09:43:58 default[20170210t104151]  module.js:471
2017-02-10 09:43:58 default[20170210t104151]      throw err;
2017-02-10 09:43:58 default[20170210t104151]      ^
2017-02-10 09:43:58 default[20170210t104151]
2017-02-10 09:43:58 default[20170210t104151]  Error: Cannot find module 'serviceAccountKey.json'
2017-02-10 09:43:58 default[20170210t104151]      at Function.Module._resolveFilename (module.js:469:15)
2017-02-10 09:43:58 default[20170210t104151]      at Function.Module._load (module.js:417:25)
2017-02-10 09:43:58 default[20170210t104151]      at Module.require (module.js:497:17)
2017-02-10 09:43:58 default[20170210t104151]      at require (internal/module.js:20:19)
2017-02-10 09:43:58 default[20170210t104151]      at Object.<anonymous> (/app/server.js:7:22)
2017-02-10 09:43:58 default[20170210t104151]      at Module._compile (module.js:570:32)
2017-02-10 09:43:58 default[20170210t104151]      at Object.Module._extensions..js (module.js:579:10)
2017-02-10 09:43:58 default[20170210t104151]      at Module.load (module.js:487:32)
2017-02-10 09:43:58 default[20170210t104151]      at tryModuleLoad (module.js:446:12)
2017-02-10 09:43:58 default[20170210t104151]      at Function.Module._load (module.js:438:3)
2017-02-10 09:44:10 default[20170210t104151]  module.js:471
2017-02-10 09:44:10 default[20170210t104151]      throw err;
2017-02-10 09:44:10 default[20170210t104151]      ^
2017-02-10 09:44:10 default[20170210t104151]
2017-02-10 09:44:10 default[20170210t104151]  Error: Cannot find module 'serviceAccountKey.json'
2017-02-10 09:44:10 default[20170210t104151]      at Function.Module._resolveFilename (module.js:469:15)
2017-02-10 09:44:10 default[20170210t104151]      at Function.Module._load (module.js:417:25)
2017-02-10 09:44:10 default[20170210t104151]      at Module.require (module.js:497:17)
2017-02-10 09:44:10 default[20170210t104151]      at require (internal/module.js:20:19)
2017-02-10 09:44:10 default[20170210t104151]      at Object.<anonymous> (/app/server.js:7:22)
2017-02-10 09:44:10 default[20170210t104151]      at Module._compile (module.js:570:32)
2017-02-10 09:44:10 default[20170210t104151]      at Object.Module._extensions..js (module.js:579:10)
2017-02-10 09:44:10 default[20170210t104151]      at Module.load (module.js:487:32)
2017-02-10 09:44:10 default[20170210t104151]      at tryModuleLoad (module.js:446:12)
2017-02-10 09:44:10 default[20170210t104151]      at Function.Module._load (module.js:438:3)
2017-02-10 16:20:02 default[20170210t124920]  Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "connect ETIMEDOUT 74.125.202.84:443".
2017-02-10 16:20:02 default[20170210t124920]      at /app/node_modules/firebase-admin/lib/firebase-app.js:74:23
2017-02-10 16:20:02 default[20170210t124920]      at process._tickCallback (internal/process/next_tick.js:103:7)

How do I fix this ??

Answer

Adam picture Adam · Mar 6, 2017

These issues seem to be caused by a misunderstanding of how require() paths work. You cannot use an absolute path because something like require("/Users/username/somepath") obviously won't exist on the remote machine when the app is deployed and the import will fail. Using require("serviceAccountKey.json") is going to look in node_modules/serviceAccountKey.json relative to the app directory.

If you want to load serviceAccountKey.json from the app's root directory, you would use require("./serviceAccountKey.json"). If it were in a subdirectory called foo under the root you'd use require("./foo/serviceAccountKey.json"). This also applies to loading modules in general such as firebase-admin.

The NodeJS Modules documentation explains the require() mechanism in greater detail.