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

前端 未结 3 1121
悲哀的现实
悲哀的现实 2021-02-05 13:06

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 secti

相关标签:
3条回答
  • 2021-02-05 13:21

    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.

    0 讨论(0)
  • 2021-02-05 13:21

    I had a similar problem running 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.

    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,

    0 讨论(0)
  • 2021-02-05 13:38

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

    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);
    
    0 讨论(0)
提交回复
热议问题