NCT 127
NCT 127

Reputation: 676

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

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
}

Upvotes: 22

Views: 15041

Answers (11)

Cameron McClymont
Cameron McClymont

Reputation: 95

For me, the issue was caused by a relative import for something outside the functions folder:

import ... from "../../../src/something.ts"

Everything worked when I removed this line, deleted my functions/lib folder, and ran npm run build again in my functions folder.

Upvotes: 0

Alexander Ferreras
Alexander Ferreras

Reputation: 1698

Run npm run build and it will create the folder lib

Upvotes: 1

Reid Moffat
Reid Moffat

Reputation: 361

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...
        }
    ]
}

Upvotes: 0

Dante Arrighi
Dante Arrighi

Reputation: 11

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.

Upvotes: 1

Felix K.
Felix K.

Reputation: 15643

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.

Upvotes: 6

yasin
yasin

Reputation: 31

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

{
  "functions": {
    "predeploy": ["npm --prefix ./functions run build"],
    "source": "functions"
  }
}

Upvotes: 0

Edward Amoah Idun
Edward Amoah Idun

Reputation: 311

cd into your functions folder and run this command

npm run-script build

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

Upvotes: 21

eliassn
eliassn

Reputation: 59

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

Upvotes: 2

Ali80
Ali80

Reputation: 8626

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

"main": "lib/src/index.js",

Upvotes: 15

Boris Strandjev
Boris Strandjev

Reputation: 46943

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.

Upvotes: 5

NCT 127
NCT 127

Reputation: 676

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

Upvotes: 0

Related Questions