火力基地部署;导致 "Cannot use import outside of an module" [不重复]

firebase deploy ; causing "Cannot use import outside of an module" [NOT DUPLICATE]

注意:这个问题可能看起来像一个重复的问题,但它是 not/tried 所有的修复,但仍然不起作用! (看了题目就知道为什么了)

所以,我有一个 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 在 functions 文件夹中,并添加类型:模块,类似这样的东西,

functions/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 中有类型:模块,但仍然出现“无法使用导入”错误。我已经尝试重新部署它并使用 firebase shell 并且一切正常但无法部署它。

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

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

尝试改用 require。

const functions = require('firebase-functions')

我们遇到了同样的问题,但没有做任何更改。它似乎是一个更新的运行 node --check 的 buildpack 配置,它反过来不支持 package.json 中的模块设置。您可以在 GCP 控制台的 Cloud Build 日志中找到更多详细信息。

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

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

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

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