15

第一次在这里部署 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 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)

我该如何解决 ??

4

3 回答 3

42

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

如果你想从应用的根目录加载serviceAccountKey.jsonrequire("./serviceAccountKey.json") ,你可以使用. 如果它位于根目录下名为foo的子目录中,您将使用require("./foo/serviceAccountKey.json"). 这也适用于加载模块,例如firebase-admin.

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

于 2017-03-06T19:49:23.590 回答
4

我可以像这样解决这个过程:(在 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);
于 2017-03-05T23:54:24.783 回答
1

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

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

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

如果其他人有同样的问题,我建议在package.json. dependencies需要运行,devDependencies仅用于开发,例如:单元测试、Coffeescript 到 Javascript 的转译、缩小、

于 2017-06-18T10:26:08.777 回答