【问题标题】:Firebase Functions Backend Error: Cannot Import LibraryFirebase 函数后端错误:无法导入库
【发布时间】:2021-02-09 09:47:48
【问题描述】:

昨天晚上我部署了我的函数,部署成功,没有任何错误。

但是当我尝试执行/调用该函数时,它会抛出以下错误并登录到 firebase 控制台:

{ Error: Failed to import the Cloud Storage client library for Node.js. Make sure to install the "@google-cloud/storage" npm package. Original error: SyntaxError: Unexpected token {
    at new FirebaseError (/srv/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at new Storage (/srv/node_modules/firebase-admin/lib/storage/storage.js:65:19)
    at /srv/node_modules/firebase-admin/lib/firebase-app.js:255:20
    at FirebaseApp.ensureService_ (/srv/node_modules/firebase-admin/lib/firebase-app.js:376:23)
    at FirebaseApp.storage (/srv/node_modules/firebase-admin/lib/firebase-app.js:253:21)
    at FirebaseNamespace.fn (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:292:45)
    at Object.exports.processCard (/srv/Files/process.js:157:24)
    at Busboy.bus.on (/srv/index.js:44:13)
    at emitNone (events.js:106:13)
    at Busboy.emit (events.js:208:7)
  errorInfo: 
   { code: 'storage/missing-dependencies',
     message: 'Failed to import the Cloud Storage client library for Node.js. Make sure to install the "@google-cloud/storage" npm package. Original error: SyntaxError: Unexpected token {' } }  

之后我尝试了更多时间,以检查错误是否消失,但随后抛出以下错误:

Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.
    at FirebaseAppError.FirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:43:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/srv/node_modules/firebase-admin/lib/utils/error.js:89:28)
    at new FirebaseAppError (/srv/node_modules/firebase-admin/lib/utils/error.js:124:28)
    at FirebaseNamespaceInternals.initializeApp (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:68:23)
    at FirebaseNamespace.initializeApp (/srv/node_modules/firebase-admin/lib/firebase-namespace.js:423:30)
    at Object.exports.processCard (/srv/Files/process.js:140:15)
    at Busboy.bus.on (/srv/index.js:44:13)
    at emitNone (events.js:106:13)
    at Busboy.emit (events.js:208:7)
    at Busboy.emit (/srv/node_modules/busboy/lib/main.js:37:33)
  errorInfo: 
   { code: 'app/duplicate-app',
     message: 'The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.' },
  codePrefix: 'app' }  

我在本地执行了这个函数,一切都很好,没有任何错误。

奇怪的部分是我从未真正使用过google-storage 库我使用firebase-admin sdk 来生成signedURL,这是生成singedURL 的功能:

admin = require('firebase-admin');
admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: "urlToDatabase"
   });

function getSignedUrl(file) {
    console.time('URLGenerated ');
    let options = { action: 'read', expires: Date.now() + 5 * 60 * 1000 }; // 5min Expiration Time
    let bucketFileName = path.basename(file);
    return bucket.upload(file, { destination: `public/${bucketFileName}`})
        .then(() => {
            return bucket.file(`public/${bucketFileName}`).getSignedUrl(options)
                .then((urls) => {
                    fs.unlinkSync(file);
                    console.timeEnd('URLGenerated ');
                    return urls[0];
                })
                .catch((e) => {
                    console.log('Link Generation Error' + e);
                });
        }).catch((e) => console.log(e));
} 

在第二个错误中,它说我已经初始化了 admin 两次,但我认为我没有这样做。我仔细检查了evything,还尝试了我之前提到的模拟器。

这是 package.json:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "index.js",
  "dependencies": {
    "@google-cloud/storage": "^5.3.0",
    "busboy": "^0.3.1",
    "docx": "^5.3.0",
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "gs4fb": "^1.1.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

现在是 20 小时,我的功能已关闭。功能是我网站的核心。

问题出在哪里?

我已经更新到最新版本的firebase-adminfirebase-functions 库和firebaseCLI,不是因为我需要它,只是因为它每次都在警告我。

【问题讨论】:

  • 请发布存储路径在哪里,我找不到存储桶分配?
  • @hey Ashish bucket = admin.storage().bucket('myapp-web.appspot.com'); 这就是我设置存储桶的方式
  • 是不是有点像:gs://myapp-web.appspot.com?
  • 不,我们只需要传递存储桶名称而不是 url 给它。并且错误是关于应用程序初始化而不是存储桶。
  • 它记录了两个错误,第一个是“无法导入库”,第二个是“多重初始化”我发现库部分很奇怪,因为我从未真正要求过它。和应用初始化一样。

标签: firebase google-cloud-functions firebase-storage firebase-hosting


【解决方案1】:

最新版本的firebase-Admin 导致错误。回滚到8.13,一切正常。

说真的如果不需要,永远不要更新到最新版本

【讨论】:

猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-13
相关资源
最近更新 更多