Google App Engine - 节点:无法找到模块'firebase-admin' [英] Google App Engine - Node: Cannot find module 'firebase-admin'
问题描述
第一次在这里部署GAE应用程序时,我正在尝试遵循本教程: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html (请参阅节点代码部分)。
运行
npm install firebase-admin --save
npm install request --save
在我的机器上,package.json在这里,但是当我使用 gcloud app部署
,我在浏览器中打开应用程序时获得这些日志:
2017-02-10 09:35:02默认[20170210t103151] module.js:471
2017-02-10 09:35:02默认[20170210t103151]抛出错误;
2017-02-10 09:35:02预设[20170210t103151] ^
2017-02-10 09:35:02预设[20170210t103151]
2017-02-10 09:35: 02默认[20170210t103151]错误:找不到模块'firebase-admin'
2017-02-10 09:35:02默认[20170210t103151]在Function.Module._resolveFilename(module.js:469:15)
2017-02-10 09:35:02默认[20170210t103151]在Function.Module._load(module.js:417:25)
2017-02-10 09:35:02默认[20170210t103151]在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默认[20170210t103151]位于Object。< anonymous> (/app/server.js:1:78)
2017-02-10 09:35:02默认[20170210t103151]在Module._compile(module.js:570:32)
2017-02 -10 09:35:02默认[20170210t103151]在Object.Module._extensions..js(module.js:579:10)
2017-02-10 09:35:02默认[20170210t103151] at Module。加载(module.js:487:32)
2017-02-10 09:35:02默认[20170210t103151]在tryModuleLoad(module.js:446:12)
2017-02-10 09: 35:02默认[20170210t103151]在Function.Module._load(module.js:438:3)
2017-02-10 09:35:07默认[20170210t103151] module.js:471
2017 -02-10 09:35:07默认[20170210t103151]抛出错误;
2017-02-10 09:35:07预设[20170210t103151] ^
2017-02-10 09:35:07预设[20170210t103151]
2017-02-10 09:35: 07默认[20170210t103151]错误:找不到模块'firebase-admin'
2017-02-10 09:35:07默认[20170210t103151]在Function.Module._resolveFilename(module.js:469:15)
2017-02-10 09:35:07默认[20170210t103151]在Function.Module._load(module.js:417:25)
2017-02-10 09:35:07默认[20170210t103151]在Module.require(module.js:497:17)
2017-02-10 09:35:07默认[20170210t103151]在要求(内部/ module.js:20:19)
2017-02 -10 09:35:07默认[20170210t103151]在对象上。< anonymous> (/app/server.js:1:78)
2017-02-10 09:35:07默认[20170210t103151]在Module._compile(module.js:570:32)
2017-02 -10 09:35:07默认[20170210t103151]在Object.Module._extensions..js(module.js:579:10)
2017-02-10 09:35:07默认[20170210t103151] at Module。 load(module.js:487:32)
2017-02-10 09:35:07默认[20170210t103151] at tryModuleLoad(module.js:446:12)
2017-02-10 09: 35:07默认[20170210t103151]在Function.Module._load(module.js:438:3)
2017-02-10 09:43:58默认[20170210t104151] module.js:471
2017 -02-10 09:43:58默认[20170210t104151]抛出err;
2017-02-10 09:43:58预设[20170210t104151] ^
2017-02-10 09:43:58预设[20170210t104151]
2017-02-10 09:43: 58默认[20170210t104151]错误:无法找到模块'serviceAccountKey.json'
2017-02-10 09:43:58默认[20170210t104151]在Function.Module._resolveFilename(module.js:469:15)
2017-02-10 09:43:58默认[20170210t104151]在Function.Module._load(module.js:417:25)
2017-02-10 09:43:58默认[20170210t104151]在Module.require(module.js:497:17)
2017-02-10 09:43:58默认[20170210t104151] at require(internal / module.js:20:19)
2017-02 -10 09:43:58默认[20170210t104151]位于Object。< anonymous> (/app/server.js:7:22)
2017-02-10 09:43:58默认[20170210t104151]在Module._compile(module.js:570:32)
2017-02 -10 09:43:58默认[20170210t104151]在Object.Module._extensions..js(module.js:579:10)
2017-02-10 09:43:58默认[20170210t104151] at Module。加载(module.js:487:32)
2017-02-10 09:43:58默认[20170210t104151]在tryModuleLoad(module.js:446:12)
2017-02-10 09: 43:58默认[20170210t104151]在Function.Module._load(module.js:438:3)
2017-02-10 09:44:10默认[20170210t104151] module.js:471
2017 -02-10 09:44:10默认[20170210t104151]抛出错误;
2017-02-10 09:44:10预设[20170210t104151] ^
2017-02-10 09:44:10预设[20170210t104151]
2017-02-10 09:44: 10默认[20170210t104151]错误:无法找到模块'serviceAccountKey.json'
2017-02-10 09:44:10默认[20170210t104151]在Function.Module._resolveFilename(module.js:469:15)
2017-02-10 09:44:10默认[20170210t104151]在Function.Module._load(module.js:417:25)
2017-02-10 09:44:10默认[20170210t104151] at Module.require(module.js:497:17)
2017-02-10 09:44:10默认[20170210t104151] at require(internal / module.js:20:19)
2017-02 -10 09:44:10默认[20170210t104151]在对象上。< anonymous> (/app/server.js:7:22)
2017-02-10 09:44:10默认[20170210t104151]在Module._compile(module.js:570:32)
2017-02 -10 09:44:10默认[20170210t104151]在Object.Module._extensions..js(module.js:579:10)
2017-02-10 09:44:10默认[20170210t104151] at Module。 load(module.js:487:32)
2017-02-10 09:44:10默认[20170210t104151] at tryModuleLoad(module.js:446:12)
2017-02-10 09: 44:10默认[20170210t104151]在Function.Module._load(module.js:438:3)
2017-02-10 16:20:02默认[20170210t124920]错误:提供给initializeApp()的凭证实现通过凭证属性无法获取有效的Google OAuth2访问令牌,并出现以下错误:connect ETIMEDOUT 74.125.202.84:443。
2017-02-10 16:20:02默认[20170210t124920]在/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)
我该如何解决这个问题?
这些问题似乎是由于误解 require()
路径工作。您不能使用绝对路径,因为在部署应用程序时,远程计算机上显然不存在 require(/ Users / username / somepath)
>并且导入将失败。使用 require(serviceAccountKey.json)
将会在相对于app目录的 node_modules / serviceAccountKey.json 中查找。
如果您想从应用程序的根目录加载 serviceAccountKey.json ,您可以使用 require(./ serviceAccountKey.json)
。如果它位于根目录下名为 foo 的子目录下,您可以使用 require(./ foo / serviceAccountKey.json)
。这也适用于加载一般的模块,例如 firebase-admin
。
NodeJS Modules文档更详细地解释了 require()
机制。
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 ??
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.
这篇关于Google App Engine - 节点:无法找到模块'firebase-admin'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!