0

With Firebase Cloud Functions 2nd Generation, I should be able to use ES 6 style imports like

//index.js

import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
...

And in fact, the above imports do work properly.

However, when I try to import a function from a local .js file, I get an error on deployment.

Project structure

functions/
  index.js
  myfuncs.js

package.json
(Note the file: prefix as described here)

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "type": "module",
  "scripts": {
    "lint": "eslint",
    "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": "18"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^11.8.0",
    "firebase-functions": "^4.3.1",
    "myfuncs": "file:./"
  },
 ...
}
//index.js

import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { myFunc } from "./myfuncs"

Firebase CLI

$ firebase deploy --only functions

Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error

If I copy + paste myfunc into index.js, the deployment runs without error. In other words, I don't believe there is a syntax error with my function.

Am I attempting something that is not possible?

Ben
  • 20,038
  • 30
  • 112
  • 189

1 Answers1

0

Finally got this to work. The thing I was missing was that I needed to create a node package.

  1. Restructure my project from this

    functions/
      index.js
      myfuncs.js
      package.json
    

    to this

    functions/
      index.js
      myfuncs/
        myfuncs.js
    
  2. Initialize the package with npm init
    (This creates the file myfuncs/package.json)

  3. Deal with this

  4. Install the package npm install -S ./myfuncs
    (This should add "myfuncs": "file:myfuncs" to the firebase package.json)

  5. Import

    //index.js
    import { initializeApp } from "firebase-admin/app";
    import { getFirestore } from "firebase-admin/firestore";
    import { myFunc } from "myfuncs"
    

    ^ Notice this is no longer a relative import from "./myfuncs"

Ben
  • 20,038
  • 30
  • 112
  • 189
  • You shouldn't have to do this. You can import a JavaScript file as you normally would, and it has nothing to do with Cloud Functions. It's not clear to me why you originally added `"myfuncs": "file:./"` to your package.json. You don't need to add anything to package.json to import anything at all that lives adjacent to the code doing the import. In fact, that line in package.json might have been preventing a normal file-based import of the same name from working at all. – Doug Stevenson Jul 03 '23 at 22:05
  • Hey thanks Doug, I thought it should work too but it does not. I just tested this again. My import statement looks like `import { myfunc } from "./myfuncs";` The exact error I get is "i functions: Loading and anaylzing source code for codebase default to determine what to deploy Serving at port 8137 shutdown requested via /__/quitquitquit Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error" [Screenshot here](https://imgur.com/a/U1udU4l) – Ben Jul 04 '23 at 03:16
  • On second look, I think think is a better job for babel or TypeScript to transpile the imports into the code that that actually gets deployed. – Doug Stevenson Jul 04 '23 at 13:49