web-dev-qa-db-fra.com

Google App Engine - Nœud: impossible de trouver le module "firebase-admin"

La première fois que je déploie une application GAE ici, j'essaie de suivre ce tutoriel: https://firebase.googleblog.com/2016/08/sending-notifications-between-Android.html (voir noeud section de code).

L'Iran

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

sur ma machine, le package.json est là, mais quand j'utilise gcloud app deploy, J'obtiens ces journaux lors de l'ouverture de l'application sur mon navigateur:

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)

Comment puis-je réparer ça ??

13
ypicard

Ces problèmes semblent être dus à une mauvaise compréhension du fonctionnement des chemins require(). Vous ne pouvez pas utiliser un chemin absolu car quelque chose comme require("/Users/username/somepath") n'existera évidemment pas sur la machine distante lorsque l'application est déployée et l'importation échouera. L'utilisation de require("serviceAccountKey.json") va chercher dans node_modules/serviceAccountKey.json par rapport au répertoire de l'application.

Si vous souhaitez charger serviceAccountKey.json à partir du répertoire racine de l'application, vous utiliserez require("./serviceAccountKey.json"). Si c'était dans un sous-répertoire appelé foo sous la racine, vous utiliseriez require("./foo/serviceAccountKey.json"). Cela s'applique également au chargement de modules en général tels que firebase-admin.

La documentation des modules NodeJS explique le mécanisme require() plus en détail.

30
Adam

Je pourrais faire une solution de contournement sur cette procédure comme ceci: (passage des paramètres serviceAccountkeyFile.json dans le code app.js):

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: "[email protected]",
        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);
4
Marcelo Gumiero

J'ai rencontré un problème similaire lors de l'exécution de gcloud app deploy

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

J'ai résolu le problème en déplaçant la dépendance donnée (wepack-dev-server) de devDependencies vers dependencies dans package.json.

Dans le cas où quelqu'un d'autre aurait le même problème, je suggère de regarder vos dépendances dans package.json. dependencies sont nécessaires pour fonctionner, devDependencies uniquement pour développer, par exemple: tests unitaires, transpilation de Coffeescript vers Javascript, minification,

0
Titogelo