DEV Community

Will Ceolin
Will Ceolin

Posted on • Updated on

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

Sometimes when you're deploying a monorepo to Cloud Functions for Firebase, you'll get the following error:

Error: There was an error reading functions/package.json:
 functions/lib/index.js does not exist, can't deploy Cloud Functions
Enter fullscreen mode Exit fullscreen mode

In some cases, that's happening because you're importing external types and Typescript is adding them to the compiled lib folder. Therefore, Firebase isn't able to find your functions because the default path is wrong.

Because Firebase uses the main field from your functions/package.json file, you just have to update it with the correct compiled path:

{
  "main": "lib/functions/src/index.js"
}
Enter fullscreen mode Exit fullscreen mode

Follow me on Twitter

Top comments (8)

Collapse
 
jaiko86 profile image
jaiko86

Hey, I've been trying to debug my problem for like the past 3 hours, and your simple post saved me.

Like most Firebase projects, my project is structured like this:

/
├──functions
│   ├──lib
│   └──src
│       └──index.ts
├──src
│   └──...
└──types
    ├──shared-type-1.ts
    ├──shared-type-2.ts
    └──...
Enter fullscreen mode Exit fullscreen mode

shared-type-1.ts has TypeScript types that are used in both the /src and /functions. So I'm importing shared-type-1.ts from /functions/index.ts. Because of this, when the /functions folder builds and outputs the result in /lib, it outputs it like this:

/
├──functions
│   ├──lib
│   │   ├──functions
│   │   │   └──src
│   │   │       └──index.js
│   │   └──types
│   │       └──shared-type-1.type.js
│   └──src
│       └──index.ts
├──src
│   └──...
└──types
    ├──shared-type-1.ts
    ├──shared-type-2.ts
    └──...
Enter fullscreen mode Exit fullscreen mode

As you can see, the transpiled index.html moved to a different folder within /lib, so I had to update the main entry in the package.json as you suggested.

Thanks.

Collapse
 
methodician profile image
Jacob Johnston

Awesome! This must be the fourth or fifth time I have been all like "WTF" and needed this fix, but each time separated by years so I needed to go searching for it. For me it comes from a love of strong typing and wanting to share models between the front end and back end code. It fails to deploy and I remember that this practice changes my compiled functions file structure, then go searching around the firebase config files trying to fix it. For some reason I always forget that it comes from package.json even though the error seems to indicate that. I guess more causes than just deploying a monorepo, but same basic fix.

Collapse
 
ariel profile image
MNSV~スロー

how you do it?

Collapse
 
graunephar profile image
Daniel Graungaard

Thank you! I still had the old index file on my local machine. So used so much time figuring out why it failed on deploy. Your post explained it perfectly, and made it possible for me to fix my problem! Thank you for that!

Collapse
 
codewithjohnson profile image
Muyiwa Johnson

Thank you !

Collapse
 
help_me_iam_verking profile image
makinggainz

This did not work for me.

Collapse
 
help_me_iam_verking profile image
makinggainz
Collapse
 
dondozone profile image
DonDozone

Thanks for the information! It was driving me nuts why it didn't work anymore, after I did some very minor type changes :D