19

When I try deploying my Firebase cloud functions I get the following error.

Desired behavior: Deploy functions successfully.

Error:

Error: There was an error reading functions/package.json:

functions/lib/index.js does not exist, can't deploy Cloud Functions

Full log:

name@name-MacBook-Pro functions % firebase deploy

=== Deploying to 'newtiktok-21570'...

i deploying functions Running command: npm --prefix "$RESOURCE_DIR" run lint

functions@ lint /Users/name/Desktop/Yoveo/functions eslint "src/**/*"

/Users/name/Desktop/Yoveo/functions/src/index.ts
186:67 warning 'timestamp' is defined but never used
@typescript-eslint/no-unused-vars 377:86 warning 'mediaNum' is defined but never used @typescript-eslint/no-unused-vars 377:104 warning 'commentText' is defined but never used @typescript-eslint/no-unused-vars 377:125 warning 'commentID' is defined but never used @typescript-eslint/no-unused-vars 419:119 warning 'commentID' is defined but never used
@typescript-eslint/no-unused-vars 463:121 warning 'commentID' is defined but never used @typescript-eslint/no-unused-vars 520:75
warning 'mediaNum' is defined but never used
@typescript-eslint/no-unused-vars 732:25 warning 'slap' is defined but never used @typescript-eslint/no-unused-vars

✖ 8 problems (0 errors, 8 warnings)

Running command: npm --prefix "$RESOURCE_DIR" run build ✔ functions: Finished running predeploy script.

Error: There was an error reading functions/package.json:

My p.json:

 {
  "name": "functions",
  "scripts": {
    "lint": "eslint \"src/**/*\"",
    "build": "",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "12"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.8.1",
    "@typescript-eslint/parser": "^4.8.1",
    "eslint": "^7.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}
NCT 127
  • 596
  • 1
  • 6
  • 27
  • Please have a look into this [Official Documentation](https://stackoverflow.com/a/48603033/8791788). Please execute **npm install** from functions folder and deploy again. Could you please share your directory structure? Please also have a look into the Firebase Official [Documentation](https://firebase.google.com/docs/functions/local-emulator#run_the_emulator_suite). – Nibrass H Nov 23 '20 at 16:38

9 Answers9

20

cd into your functions folder and run this command

npm run-script build

This will create the lib/index.js file that is missing

H3AR7B3A7
  • 4,366
  • 2
  • 14
  • 37
10

firebase uses main field in package.json as program entry point, set it properly, probably like this.

"main": "lib/src/index.js",
Ali80
  • 6,333
  • 2
  • 43
  • 33
4

For some reason recently the build flow of firebase functions changed.

It used to be:

npm --prefix ./functions install ./functions
firebase deploy --only functions

now it is:

npm --prefix ./functions install ./functions
npm --prefix ./functions run build
firebase deploy --only functions

I have not researched what caused this change, but adding this as build step fixed the problem for me.

Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
3

functions/lib/index.js does not exist

In case you are working in firebase project that contains a frontend or is structured as a monorepo, this error may also stem from having accidentally imported a frontend file in the functions backend part of the project. For all files that are not within your functions project scope, the typescript compiler will refuse to compile ts files referencing them. So in this case, the solution is to search for any imports containing /src/ (or any other paths pointing outside) and remove (or correct) them within your functions project.

Felix K.
  • 14,171
  • 9
  • 58
  • 72
  • 1
    In my case, Visual Studio Code's auto-include modified my `tsconfig.json` and added a reference to a file outside of the firebase tree... This indeed caused the problem. Thanks for the answer! – Yuval Sep 10 '22 at 18:49
1

you just have to change the main inside package.json file from lib/index.js to your index file which is usually under the src folder

eliassn
  • 39
  • 5
0

Solved:

I was able to solve the problem by removing everything associated with Firebase functions. And running: firebase init again. After I cd functions run npm install. Then I was able to deploy successfully after fixing an error with:

    3:26  error    'express' should be listed in the project's dependencies. Run 'npm i -S express' to add it  import/no-extraneous-dependencies
NCT 127
  • 596
  • 1
  • 6
  • 27
0

Changing the firebase.json file to the following fixed my issue:

{
  "functions": {
    "predeploy": ["npm --prefix ./functions run build"],
    "source": "functions"
  }
}
yasin
  • 31
  • 1
  • 7
0

I was able to fix this same issue by following Felix K indications, answered on Apr 28, 2021.

In case you are working in firebase project that contains a frontend or is structured as a monorepo, this error may also stem from having accidentally imported a frontend file in the functions backend part of the project. For all files that are not within your functions project scope, the typescript compiler will refuse to compile ts files referencing them. So in this case, the solution is to search for any imports containing /src/ (or any other paths pointing outside) and remove (or correct) them within your functions project.

In my case, I've accidently imported an interface from the frontend. When updating this import I was able to successfully deploy my function.

Solution from Edward Amoah Idun:

cd into your functions folder and run this command
npm run-script build
This will create the lib/index.js file that is missing

Yes, but it will create the index.js file that is missing in the wrong folder. Still necessary to check that you don't have imports from another projects.

0

The lib folder is for your built functions code, so you haven't built it. This can be done automatically by adding redeploy code to your firebase.json config file:

{
    "functions": [
        {
            "predeploy": ["npm --prefix \"$RESOURCE_DIR\" run build"],
            // rest of config...
        }
    ]
}
Reid Moffat
  • 322
  • 1
  • 3
  • 16