Google App Engine - 节点:找不到模块'firebase-admin'

时间:2021-11-03 20:07:36

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).

首次在此部署GAE应用,我正在尝试按照本教程进行操作:https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html(请参阅节点代码部分)。

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:

在我的机器上,package.json就在这里,但是当我使用gcloud app deploy时,我在浏览器上打开应用程序时会得到这些日志:

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 ??

我该如何解决 ??

3 个解决方案

#1


15  

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.

这些问题似乎是由于对require()路径如何工作的误解造成的。您不能使用绝对路径,因为当部署应用程序并且导入失败时,远程机器上显然不会存在类似require(“/ Users / username / somepath”)的内容。使用require(“serviceAccountKey.json”)将相对于app目录查看node_modules / serviceAccountKey.json。

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.

如果要从应用程序的根目录加载serviceAccountKey.json,可以使用require(“./ serviceAccountKey.json”)。如果它位于根目录下名为foo的子目录中,则使用require(“./ foo / serviceAccountKey.json”)。这也适用于加载模块,例如firebase-admin。

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

NodeJS模块文档更详细地解释了require()机制。

#2


1  

I could make a workaround on this proceeding like this: (passing the serviceAccountkeyFile.json parameters inside app.js code):

我可以像这样对这个过程做一个解决方法:(在app.js代码中传递serviceAccountkeyFile.json参数):

let defaultAppConfig = {
    credential: admin.credential.cert({
        type: "service_account",
        project_id: "txxxxxxxxxx",
        private_key_id: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
        private_key: "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n",
        client_email: "firebaxxxxxxx@xxxxxxx.gserviceaccount.com",
        client_id: "11111111111111111",
        auth_uri: "https://accounts.google.com/o/oauth2/auth",
        token_uri: "https://accounts.google.com/o/oauth2/token",
        auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
        client_x509_cert_url: "https://www.googleapis.com/robot/v1/metadata/x509/jjjjjj.gserviceaccount.com"
    }),
    databaseURL: 'https://projectxxx.firebaseio.com/'
}

defaultApp = admin.initializeApp(defaultAppConfig);

#3


0  

I had a similar problem running gcloud app deploy

我在运行gcloud app deploy时遇到了类似的问题

Error: Cannot find module 'webpack-dev-server'
at Function.Module._resolveFilename (module.js:469:15)

I solved the issue moving the given dependency (wepack-dev-server) from devDependencies to dependencies in package.json.

我解决了将给定依赖项(wepack-dev-server)从devDependencies移动到package.json中的依赖项的问题。

In case somebody else has the same problem I suggest to have look at your dependencies in package.json. dependencies are required to run, devDependencies only to develop, e.g.: unit tests, Coffeescript to Javascript transpilation, minification,

如果其他人有同样的问题,我建议你在package.json中查看你的依赖项。运行时需要依赖项,devDependencies只用于开发,例如:单元测试,Coffeescript到Javascript的编译,缩小,

#1


15  

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.

这些问题似乎是由于对require()路径如何工作的误解造成的。您不能使用绝对路径,因为当部署应用程序并且导入失败时,远程机器上显然不会存在类似require(“/ Users / username / somepath”)的内容。使用require(“serviceAccountKey.json”)将相对于app目录查看node_modules / serviceAccountKey.json。

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.

如果要从应用程序的根目录加载serviceAccountKey.json,可以使用require(“./ serviceAccountKey.json”)。如果它位于根目录下名为foo的子目录中,则使用require(“./ foo / serviceAccountKey.json”)。这也适用于加载模块,例如firebase-admin。

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

NodeJS模块文档更详细地解释了require()机制。

#2


1  

I could make a workaround on this proceeding like this: (passing the serviceAccountkeyFile.json parameters inside app.js code):

我可以像这样对这个过程做一个解决方法:(在app.js代码中传递serviceAccountkeyFile.json参数):

let defaultAppConfig = {
    credential: admin.credential.cert({
        type: "service_account",
        project_id: "txxxxxxxxxx",
        private_key_id: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
        private_key: "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n",
        client_email: "firebaxxxxxxx@xxxxxxx.gserviceaccount.com",
        client_id: "11111111111111111",
        auth_uri: "https://accounts.google.com/o/oauth2/auth",
        token_uri: "https://accounts.google.com/o/oauth2/token",
        auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
        client_x509_cert_url: "https://www.googleapis.com/robot/v1/metadata/x509/jjjjjj.gserviceaccount.com"
    }),
    databaseURL: 'https://projectxxx.firebaseio.com/'
}

defaultApp = admin.initializeApp(defaultAppConfig);

#3


0  

I had a similar problem running gcloud app deploy

我在运行gcloud app deploy时遇到了类似的问题

Error: Cannot find module 'webpack-dev-server'
at Function.Module._resolveFilename (module.js:469:15)

I solved the issue moving the given dependency (wepack-dev-server) from devDependencies to dependencies in package.json.

我解决了将给定依赖项(wepack-dev-server)从devDependencies移动到package.json中的依赖项的问题。

In case somebody else has the same problem I suggest to have look at your dependencies in package.json. dependencies are required to run, devDependencies only to develop, e.g.: unit tests, Coffeescript to Javascript transpilation, minification,

如果其他人有同样的问题,我建议你在package.json中查看你的依赖项。运行时需要依赖项,devDependencies只用于开发,例如:单元测试,Coffeescript到Javascript的编译,缩小,