火力基地部署;导致“无法在模块外部使用导入”[不重复]

Posted

技术标签:

【中文标题】火力基地部署;导致“无法在模块外部使用导入”[不重复]【英文标题】:firebase deploy ; causing "Cannot use import outside of an module" [NOT DUPLICATE] 【发布时间】:2022-01-15 19:08:12 【问题描述】:

注意:这个问题可能看起来像一个重复的问题,但它没有/尝试了所有的修复,但仍然不起作用! (如果你读了这个问题,你就会知道为什么)

所以,我有一个 firebase 函数项目,我的文件夹结构是这样的,

- functions
  - index.js
  - firebase-dubug.log
  - config.json
  - ... bunch of other files and folder which aren't necessary. 
- .firebaserc
- firebase.json

所以,当我运行这个命令时,

firebase deploy

它尝试部署,但在控制台中显示,

=== Deploying to 'project-id [hidden]'...

i  deploying functions, hosting
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  functions: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
+  functions: required API artifactregistry.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (2.4 MB) for uploading
+  functions: functions folder uploaded successfully
i  hosting[pq-store-dc918]: beginning deploy...
i  hosting[pq-store-dc918]: found 29 files in functions/build
+  hosting[pq-store-dc918]: file upload complete
i  functions: updating Node.js 16 function firebaseApp(us-central1)...

Functions deploy had errors with the following functions:
        firebaseApp(us-central1)
i  functions: cleaning up build files...

Error: There was an error deploying functions

现在显然,这个错误没有帮助,所以我进入 firebase-dubug.log 查找问题的确切原因,并找到了这个,


  "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
  "status": 
    "code": 3,
    "message": "Build failed: (node:98) Warning: To load an ES module, set \"type\": \"module\" in the package.json or use the .mjs extension.\n(Use `node --trace-warnings ...` to show where the warning was created)\n/workspace/index.js:1\nimport functions from 'firebase-functions'\n^^^^^^\n\nSyntaxError: Cannot use import statement outside a module\n    at Object.compileFunction (node:vm:352:18)\n    at wrapSafe (node:internal/modules/cjs/loader:1031:15)\n    at checkSyntax (node:internal/main/check_syntax:66:3)\n    at node:internal/main/check_syntax:39:3; Error ID: d984e68f"
  ,
  "authenticationInfo": 
    "principalEmail": "[email hidden]"
  ,
  "serviceName": "cloudfunctions.googleapis.com",
  "methodName": "google.cloud.functions.v1.CloudFunctionsService.UpdateFunction",
  "resourceName": "projects/[project-id, hidden]/locations/us-central1/functions/firebaseApp"

所以,我认为错误是由于没有将“type”:“module”放入 package.json 并使用 import/export 而不是 require() 引起的,所以我去了函数文件夹中的 package.json,并添加了类型:模块,类似这样的,

函数/package.json,


  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "type": "module", // I already have this in my package.json
  "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": "16"
  ,
  "main": "index.js",
  "dependencies": 
    // ...dependencies
  ,
  "private": true

我的 index.js 看起来像这样,

import functions from 'firebase-functions' // the error is caused here
import express from 'express'

// ...code which are not required for this issue :)

// Listening to port
app.listen(port, () => console.log(`Listening to port $port`))

// Configuring firebase
export const firebaseApp = functions.https.onRequest(app)

结论:我的 package.json 中有 type: module 但仍然出现“无法使用导入”错误。我尝试重新部署它并使用 firebase shell,一切正常,但无法部署。

还有一件事:我不想将它们全部转换为 require(),因为这将花费很长时间,因为该项目中有超过 200 个文件!

感谢您提前阅读和回答:)

【问题讨论】:

Firebase 功能不需要 app.listenonRequest 已经在听了。您只需像您正在做的那样将app 传递给onRequest,但没有使用listen“开始”表达。不确定这是否会导致您的问题,但请先尝试不这样做。 @I'm_Joe_Too 不幸的是,这不是问题,但感谢您的提示 【参考方案1】:

我们有同样的问题,没有改变任何东西。它似乎是运行 node --check 的更新的 buildpack 配置,而这又不遵守 package.json 中的模块设置。您可以在 GCP 控制台的 Cloud Build 日志中找到更多详细信息。

我们已向 Firebase 提交了支持请求。请你也举报一个。 https://firebase.google.com/support/troubleshooter/contact

【讨论】:

有一个临时的解决方案是使用 engine:14 而不是 16。在 14 日节点模块解析作为例外工作。 谷歌的当前票在这里github.com/GoogleCloudPlatform/functions-framework-nodejs/…【参考方案2】:

根据https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/407的说法,根源问题来自最新版本的node 16。

在撰写此答案时,该修复程序已合并到节点 16,但尚未发布。一旦发布,Google Cloud Functions 平台将很快更新 node 16 运行时的版本。

不幸的是,目前唯一的解决方法似乎是恢复使用节点 14。

【讨论】:

我的应用程序需要节点 16 才能运行,所以我将所有内容都转换为 commonJS :( 这花了几个小时【参考方案3】:

尝试使用 require。

const functions = require('firebase-functions')

【讨论】:

请阅读最后一行,我想要它而不需要,因为在超过 200 个文件中转换为需要需要大量时间 抱歉跳过了你的那部分问题。不久前我遇到了同样的问题,这里没有太多选择。在 import-vs-require 上查看此 SO 帖子:***.com/a/55998152/8723748。此外,避免文字墙式问题。虽然我知道尽可能多地提供有关某个问题的信息很有吸引力,但这类问题会阻止人们做出回应。 感谢您的帮助,我还没有尝试过,将尽快尝试。但是我仍然不明白一件事,这个问题只是随机发生的。在过去的半年里,我在 Firebase 上部署了 50 多个版本,一切正常,但突然间,我遇到了这个问题。我也没有对代码做任何奇怪的事情,但我只记得,我最近实际上将 firebase-tools 更新到了最新版本。我会尝试降级并尝试。这也可能解决它 这听起来可能会导致这种情况。或者,如果您在构建中更改了任何内容,即之前使用 webpack 而您没有,那将是另一个潜在原因。 我正在使用将构建到构建文件夹的反应,并将该文件夹复制到函数目录中,即express 应用程序服务于 react 应用程序,这几个月都可以正常工作,但突然停止了。今天我会尝试一下,看看它是否修复,我会告诉你

以上是关于火力基地部署;导致“无法在模块外部使用导入”[不重复]的主要内容,如果未能解决你的问题,请参考以下文章

FirebaseError:权限缺失或不足。 -类星体和火力基地

无法连接到火力基地

从火力基地检索信息

火力基地规则有问题

颤振和火力基地和子集合

我必须从哪里找到 DatabaseEvent 类? (颤振)(火力基地)