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

Time:12-14

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 :)

CodePudding user response:

Try using require instead.

const functions = require('firebase-functions')

CodePudding user response:

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

  • Related