Reputation: 175
NOTE: This question might seem like a duplicate question but it is not/tried all the fix but still does not work! (If you read the question, you will get to know why)
So, I have a firebase functions project, and my folder structure is something like this,
- functions
- index.js
- firebase-dubug.log
- config.json
- ... bunch of other files and folder which aren't necessary.
- .firebaserc
- firebase.json
So, when I run this command,
firebase deploy
It tries to deploy but shows this in console,
=== 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
Now obviously, this error isn't helpful, so I went into firebase-dubug.log to find the exact cause of the issue, and found this,
{
"@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"
}
So, I thought that the error was caused by not putting "type": "module" in package.json and using import/export instead of require(), so I went to package.json inside functions folder, and added type: module, something like this,
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
}
My index.js looks something like this,
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)
Conclusion: I have type: module in my package.json but still getting "cannot use import" error. I have tried re-deploying it and used firebase shell and everything worked fine but cant make it to deploy.
One more thing: I don't want to convert all of them to require() as that will take forever, as there are over 200+ files on this project!
Thanks for reading and answering in advance :)
Upvotes: 0
Views: 1738
Reputation: 540
According to https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/407, the root issue comes from the latest version of node 16.
At the time of writing this answer, the fix has been merged into node 16 but have not been released yet. Once it is released, Google Cloud Functions platform will soon update the version of node 16 runtime.
Unfortunately, the only workaround at the moment seems to be to revert to using node 14.
Upvotes: 1
Reputation: 46
We have the same issue without changing anything. It seems to be an updated buildpack config that runs node --check
which in turn doesn't honor the modules setting in package.json. You can find more details in Cloud Build logs in the GCP console.
We have filed a support ticket to Firebase. Please report one you too. https://firebase.google.com/support/troubleshooter/contact
Upvotes: 3
Reputation: 1875
Try using require instead.
const functions = require('firebase-functions')
Upvotes: 0