Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function deployment fails with npm workspaces/nx - Error: spawn ./node_modules/.bin/firebase-functions ENOENT #4952

Closed
rahuld600 opened this issue Sep 5, 2022 · 54 comments · Fixed by #5391
Assignees
Labels

Comments

@rahuld600
Copy link

rahuld600 commented Sep 5, 2022

[REQUIRED] Environment info

firebase-tools: 11.8.0 (latest release)

Platform: : macOS

[REQUIRED] Test case

If I make a function on the index.js file, and then I try to deploy using:
firebase deploy --only functions:nameOfYourFunction
I get an Unexpected Error at the end.

[REQUIRED] Steps to reproduce

Using npm or whatever package manager you prefer, run these on your terminal,

npm install -g firebase-tools
firebase login
firebase init functions

Now under the functions folder, make sure you have installed node modules using npm install,
and finally you create a basic function you like on index.js after you are done importing stuffs, for example:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.yourFunctionName = functions.https.onRequest(async(req,res)=>{
functions.logger.log(res.body); //just for an example.
})

Now when you try to do:

firebase deploy --only functions:yourFunctionName

You'll see this error message in the end:

[debug] [2022-09-05T11:21:08.962Z] Building nodejs source
[debug] [2022-09-05T11:21:08.963Z] Could not find functions.yaml. Must use http discovery
[debug] [2022-09-05T11:21:08.972Z] Error: spawn ./node_modules/.bin/firebase-functions ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:476:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
[error]
[error] Error: An unexpected error has occurred.
[debug] [2022-09-05T11:21:09.063Z] Serving at port 9005

[REQUIRED] Expected behavior

Expected behaviour is that the function gets deployed successfully without any error.

[REQUIRED] Actual behavior

Actual behaviour is that function normally get's deployed successfully and just after this update, this issue came into play.

@rahuld600 rahuld600 added the bug label Sep 5, 2022
@rahuld600
Copy link
Author

I could fix this issue just by downgrading the version of firebase tools to v10.0.0 using:
npm install -g [email protected]

@rahuld600 rahuld600 reopened this Sep 5, 2022
@rahuld600
Copy link
Author

Since there might be something wrong with the new v11.8.0, so It would be really awesome if the engineers took a look at what's up with the latest release...so I am keeping this issue open.

@rahuld600 rahuld600 changed the title Unexpected Error while deploying using "firebase deploy --only functions" in [email protected] (latest version). Unexpected Error while deploying using "firebase deploy --only functions" in [email protected] (latest version) (Could not find functions.yaml. Must use http discovery). Sep 5, 2022
@taeold
Copy link
Contributor

taeold commented Sep 6, 2022

Thanks for filing the issue @rahuld600!

QQ1 - what tooling are you using for package management (yarn? npm)? Are you usign yarn/npm workspaces?
QQ2 - have you run npm i before deploying your function?

@taeold taeold added Needs: Author Feedback Issues awaiting author feedback api: functions labels Sep 6, 2022
@rahuld600
Copy link
Author

@taeold Hello sir,
First of all, thank you for checking out!
QQ1 - I am using npm for package management.
QQ2 - Yes sir! I ran npm install before deploying.

@vevepeter
Copy link

Thanks for filing the issue @[removed to avoid tagging]!

QQ1 - what tooling are you using for package management (yarn? npm)? Are you usign yarn/npm workspaces? QQ2 - have you run npm i before deploying your function?

Hey. If I may, what if they weren't using npm but yarn and yarn workspaces? We seem to be having the same issue, only with yarn workspaces and for firebase-tools v11.15.0.

Would it be better if I created a new issue for this for full details, or is this fine? Here are some of the other relevant(?) packages and versions:
"firebase": "9.12.1"
"firebase-admin": "^11.2.0"
"firebase-functions": "4.0.1"

@joonne
Copy link

joonne commented Oct 29, 2022

Hey 👋

I'm seeing a similarish issue with firebase-tools v11.15.0 when running firebase deploy --only functions --debug

debug output:

[2022-10-29T06:03:49.031Z] Building nodejs source
[2022-10-29T06:03:49.032Z] Could not find functions.yaml. Must use http discovery
[2022-10-29T06:03:49.195Z] Serving at port 9005

[2022-10-29T06:03:49.372Z] Got response from /__/functions.yaml Failed to generate manifest from function source: Error
[2022-10-29T06:03:49.381Z] shutdown requested via /__/quitquitquit

Error: Failed to parse build specification:
- FirebaseError Expect manifest yaml to specify a version number

I deleted the existing functions as I thought it would have something do with the versioning but no dice.

package.json contents:

{
  "name": "functions",
  "scripts": {},
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "@google-cloud/firestore": "6.4.1",
    "axios": "1.1.3",
    "firebase-admin": "11.2.0",
    "firebase-functions": "4.0.1",
    "ksuid": "3.0.0",
    "purify-ts": "1.3.0",
    "stripe": "10.15.0",
    "uuid": "9.0.0"
  },
  "devDependencies": {
    "firebase-functions-test": "3.0.0",
    "typescript": "4.8.4"
  },
  "private": true
}

@kossnocorp
Copy link
Contributor

kossnocorp commented Nov 10, 2022

I have this bug on 11.16.0, the error I see (Node.js version v14.21.1):

[debug] [2022-11-10T05:02:40.798Z] Building nodejs source
[info] i  functions: Loaded environment variables from .env. 
[debug] [2022-11-10T05:02:40.800Z] Could not find functions.yaml. Must use http discovery
[debug] [2022-11-10T05:02:40.807Z] Error: spawn ./node_modules/.bin/firebase-functions ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:277:19)
    at onErrorNT (internal/child_process.js:472:16)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)
[error] 
[error] Error: An unexpected error has occurred.

@colerogers
Copy link
Contributor

@vevepeter here's a feature request issue for mono-repos - #653

@joonne that's a different error than what the other folks are experiencing, do you mind opening another issue and use the template? thanks

@kossnocorp are you also using vanilla npm or another package manager?

@kossnocorp
Copy link
Contributor

@colerogers I use npm. I found a solution - upgrading from Node v14 to v16 helped to solve the problem.

@binodnepali
Copy link

binodnepali commented Nov 18, 2022

I could fix this issue just by downgrading the version of firebase tools to v10.0.0 using: npm install -g [email protected]

This version npm install -g [email protected] also works but higher than this version does not work atleast for me

@howbizarre
Copy link

howbizarre commented Dec 6, 2022

I have the same problem with my Nuxt app.
To avoid it, I delete the node_models folder in the output directory and reinstall it.

$ NITRO_PRESET=firebase npm run build && cd .output/server && rm -rf node_modules && npm i && cd ../../ && firebase login && firebase deploy

After that everything works normally.

Node v16.18.1
Firebase v11.16.1
Ubuntu 22.04

@JoeEarly
Copy link

JoeEarly commented Dec 8, 2022

Just rolling back to 10.8.0 has solved it for me, lost 2 days of debugging a pipeline haha

@pasza01
Copy link

pasza01 commented Dec 11, 2022

Updating firebase tool from 11.2.2 → 11.17.0 worked for me.. Spend 3 days on this. Randomly break my project due to this

@colerogers
Copy link
Contributor

Hi everyone sorry for the delay, can anyone confirm they are still seeing this exact issue in the latest release (11.18.0)? I'm not able to reproduce this on my end. Thanks

@taeold taeold added the Needs: Author Feedback Issues awaiting author feedback label Dec 14, 2022
@taeold taeold removed their assignment Dec 14, 2022
@ncuillery
Copy link

Definitely got the issue using the latest 11.19.0

Downgrading to 11.17.0 has fixed the issue.

@ChromeQ
Copy link

ChromeQ commented Dec 21, 2022

I'm also seeing the same on 11.19.0 but was working fine on 11.18.0. Also happens when running emulators:start and not just deploy.
I've found the code which it differentiates... running firebase emulators:start --only functions --debug on 11.18.0 outputs:

[2022-12-21T03:49:26.597Z] Validating nodejs source
[2022-12-21T03:49:26.923Z] checkFunctionsSDKVersion encountered error:Invalid Version:  {}
[2022-12-21T03:49:26.924Z] > [functions] package.json contents: {
  "name": "functions",
  "engines": {
    "node": "16"
  },
  "private": true,
  "type": "module",
  "version": "5.2.0",
  "dependencies": {
    "@google-cloud/functions-framework": "3.1.2",
    "array-flatten": "3.0.0",
    "firebase": "9.15.0",
    "firebase-admin": "11.4.0",
    "firebase-functions": "4.1.1",
    "lodash.merge": "4.6.2",
    "node-fetch": "3.3.0",
    "nodemailer": "6.8.0"
  }
}
[2022-12-21T03:49:26.924Z] Building nodejs source
[2022-12-21T03:49:26.925Z] Analyzing nodejs backend spec
[2022-12-21T03:49:27.523Z] Could not parse firebase-functions version '' into semver. Falling back to parseTriggers.
✔  functions: Loaded functions definitions from source: ...

Running on v11.19.0 outputs:

[2022-12-21T04:03:24.281Z] Validating nodejs source
[2022-12-21T04:03:24.585Z] > [functions] package.json contents: {
  "name": "functions",
  "engines": {
    "node": "16"
  },
  "private": true,
  "type": "module",
  "version": "5.2.0",
  "dependencies": {
    "@google-cloud/functions-framework": "3.1.2",
    "array-flatten": "3.0.0",
    "firebase": "9.15.0",
    "firebase-admin": "11.4.0",
    "firebase-functions": "4.1.1",
    "lodash.merge": "4.6.2",
    "node-fetch": "3.3.0",
    "nodemailer": "6.8.0"
  }
}
[2022-12-21T04:03:24.586Z] Building nodejs source
[2022-12-21T04:03:24.591Z] Could not find functions.yaml. Must use http discovery
[2022-12-21T04:03:24.600Z] Error: spawn ./node_modules/.bin/firebase-functions ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:285:19)
    at onErrorNT (node:internal/child_process:485:16)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Error: An unexpected error has occurred

So you can see the working version says "Could not parse firebase-functions version '' into semver. Falling back to parseTriggers" which is logged from here

Which is only happening when this.sdkVersion is not valid...
v11.18.0: this.sdkVersion === ""
v11.19.0: this.sdkVersion === 4.1.1
Change happened here - 355e9b9#diff-88e84b107a8f48a6a783275f2cef81ea7f269efbc4d2c22906217d0be811d973

This is the cause of then hitting let discovered = await discovery.detectFromYaml(this.sourceDir, this.projectId, this.runtime); and that function tries to rad the functions.yaml file which is not found causing that message:

// https://github.com/firebase/firebase-tools/blob/355e9b986ed4c947757666c08b128cf22e5cb092/src/deploy/functions/runtimes/discovery/index.ts#L43
try {
        text = await exports.readFileAsync(path.join(directory, "functions.yaml"), "utf8");
    }
    catch (err) {
        if (err.code === "ENOENT") {
            logger_1.logger.debug("Could not find functions.yaml. Must use http discovery");
        }
        else {
            logger_1.logger.debug("Unexpected error looking for functions.yaml file:", err);
        }
        return;
    }

@ChromeQ
Copy link

ChromeQ commented Jan 9, 2023

@taeold Unfortunately it seems your merged fix doesn't fix my deployment. I am using v11.20.0 (which I saw does not contain your changes) so I added them to my deps using patch-package.

And here is my command and output:
image

As you can see the other variables are being echoed ($FIREBASE_PROJECT & $GOOGLE_APPLICATION_CREDENTIALS)
And just for clarification too, this is being run from the dir printed as /home/********/lms/, the functions folder does not contain a node_modules dir (this was initially the fix as per my above comments but is no longer working)

Update:
By only downgrading firebase-tools to v11.18.0 then it successfully deploys - so I do believe this is a firebase-tools issue rather than an auth issue as the logs suggest
logs

@rendomnet
Copy link

Same error in 11.20.0

@ChromeQ
Copy link

ChromeQ commented Jan 9, 2023

Same error in 11.20.0

11.20.0 does not contain the fixes merged above.

@Robula
Copy link

Robula commented Jan 11, 2023

We have exactly the same problem, rolled back from 11.20.0 to 11.17.0 and we are able to deploy once again.
For the record we are on Node 16 using Yarn Berry 3.1.1 with workspaces and node-modules nodeLinker.

@taeold Could you please consider reopening this issue?

@cbricker-msw
Copy link

cbricker-msw commented Jan 17, 2023

In my case, the deploy works on 11.20.0 version of firebase-tools, but only when I add the --debug flag to the command
$ firebase --debug deploy --only functions
Our functions are in TS in an Nx monorepo but they have their own package.json, etc.

@mesqueeb
Copy link

For us we can only deploy on 11.17.0. Please reopen this issue.

@ChromeQ
Copy link

ChromeQ commented Jan 17, 2023

v11.19.0 is the version with the breaking changes. So downgrading to 11.18.0 should be fine. The workaround for now (if possible for your situation) is to cd into the functions dir and run npm/yarn install.
The changes @taeold has made which closed this issue ARE NOT RELEASED YET, they didn't make it into v11.20.0 so the workaround of installing node_modules inside the functions dir will still work for you in v11.19.0 and even v11.20.0.

However, even with the changes applied to my codebase, this workaround did not work and neither does "normal" deployment commands. So I do believe this issue should be reopened @taeold. Thanks

@taeold
Copy link
Contributor

taeold commented Jan 22, 2023

We finally shipped 11.21.0 that includes a fix (#5391) that addresses few, common scenarios raised in this issue.

I was made aware that there are cases where the proposed fix isn't sufficient (e.g. #5448). I'm working through it. If folks open up a new issues with description/sample of your setup as done in the issue I linked here, I'd really appreciate. Thank you all for your patience.

@decori-io
Copy link

decori-io commented Jan 22, 2023

We finally shipped 11.21.0 that includes a fix (#5391) that addresses few, common scenarios raised in this issue.

I was made aware that there are cases where the proposed fix isn't sufficient (e.g. #5448). I'm working through it. If folks open up a new issues with description/sample of your setup as done in the issue I linked here, I'd really appreciate. Thank you all for your patience.

@taeold Unfortunately, I'm still getting the same error
Error: spawn ./node_modules/.bin/firebase-functions ENOENT

(Upgraded to 11.21.0, and made sure that's the same version I'm using to start the emulator.)

@ChromeQ
Copy link

ChromeQ commented Jan 23, 2023

Btw I am also not getting any success with the latest version, I am using npm not pnpm so #5448 is not relevant to my case.
I did output the sourcePath, sdkPath and binPath and it resolves firebase-functions dependency correctly to the root node_modules` rather than the functions dir.

Btw, this is also now failing when installing functions deps by running npm install inside the functions dir - which it wasn't for versions 11.19.0 & 11.20.0. My comment #4952 (comment) has more details and is still happening with v11.21.0.
Thanks @taeold

@Jimgitsit
Copy link

After upgrading to 11.21.0, I see this error when starting the functions emulator:
Error: spawn /Users/.../functions/node_modules/.pnpm/[email protected][email protected]/node_modules/.bin/firebase-functions ENOENT
Downgraded to 11.20.0 and error went away.

@edenhermelin
Copy link

Could not find functions.yaml. Must use http discovery

same here, the error appeared after upgrading from v11.19.0 to v11.21.0
using pnpm

is there another way to fix it beside downgrading?

@ChromeQ
Copy link

ChromeQ commented Feb 1, 2023

I can confirm that v11.22.0 fixes this issue as described in #5448

@desk-pro
Copy link

As mentionned in #5482, I get following error when using v11.22.0 :

Error: Failed to find location of Firebase Functions SDK. Please file a bug on Github (https://github.com/firebase/firebase-tools/).

@TacB0sS
Copy link

TacB0sS commented Mar 13, 2023

@taeold

We have encountered this issue where randomly we are able to deploy our gen v2 function and randomly fail with User code failed to load. Cannot determine backend specification error message.

Deeper investigation shows the issue is not having the .yaml file which in turn resulted in an http call which I assumes generates that yaml file.

that http call has a timeout of 30 sec, if the yaml generation was completed under 30 sec our function would deploy otherwise fail..

attaching code snippet from the [email protected] sdk

async function detectFromPort(port, project, runtime, timeout = 30000) {
    let res;
    const timedOut = new Promise((resolve, reject) => {
        setTimeout(() => {
            reject(new error_1.FirebaseError("User code failed to load. Cannot determine backend specification"));
        }, timeout);
    });
    while (true) {
        try {
            res = await Promise.race([(0, node_fetch_1.default)(`http:https://127.0.0.1:${port}/__/functions.yaml`), timedOut]);
            break;
        }
        catch (err) {
            if ((err === null || err === void 0 ? void 0 : err.code) === "ECONNREFUSED") {
                continue;
            }
            throw err;
        }
    }
    const text = await res.text();
    logger_1.logger.debug("Got response from /__/functions.yaml", text);
    let parsed;
    try {
        parsed = yaml.load(text);
    }
    catch (err) {
        logger_1.logger.debug("Failed to parse functions.yaml", err);
        throw new error_1.FirebaseError(`Failed to load function definition from source: ${text}`);
    }
    return yamlToBuild(parsed, project, api.functionsDefaultRegion, runtime);
}

Would appreciate a quick fix for this issue.
Adam.

@Fleker
Copy link

Fleker commented Apr 5, 2023

I started encountering this in a project tonight, even after downgrading to v11.19. I can get it to succeed by commenting out a ton of my code but it consistently hits the error with my full code.

@ibnzahoor98
Copy link

I faced this issue

[debug] [2023-04-07T12:56:12.957Z] Building nodejs source
[debug] [2023-04-07T12:56:12.960Z] Could not find functions.yaml. Must use http discovery
[debug] [2023-04-07T12:56:12.975Z] Found firebase-functions binary at 'G:\Github Projects\health-2077\health-2077\dist\functions\node_modules\.bin\firebase-functions'
[debug] [2023-04-07T12:56:14.744Z] Error: spawn UNKNOWN

on firebase tools version 11.25.3. After downgrading to 10.8.0 I was able to deploy my application to firebase hosting.

Thank you @JoeEarly @binodnepali for point out what worked for you.

@ChromeQ
Copy link

ChromeQ commented Apr 7, 2023

@ibnzahoor98 downgrading to such an older version is not really a long term fix imo. Plus this issue has been fixed in v11.22.0 as I outlined above.

I'd recommend going back to this version to determine if this issue is the same cause or is a regression since then.
I've kept up with the latest version and this error has not resurfaced so you may be experiencing a different problem with the same error message. And also try to delete node modules folder completely and even the package-lock or yarn-lock file too, and ensure the node version is modern.

I can confirm that v11.22.0 fixes this issue as described in #5448

@ibnzahoor98
Copy link

@ibnzahoor98 downgrading to such an older version is not really a long term fix imo. Plus this issue has been fixed in v11.22.0 as I outlined above.

I'd recommend going back to this version to determine if this issue is the same cause or is a regression since then. I've kept up with the latest version and this error has not resurfaced so you may be experiencing a different problem with the same error message. And also try to delete node modules folder completely and even the package-lock or yarn-lock file too, and ensure the node version is modern.

I can confirm that v11.22.0 fixes this issue as described in #5448

Thank you for pointing out. I will move to 11.22.0

@Fleker
Copy link

Fleker commented Apr 14, 2023

I have just updated to 11.27.0 and am still seeing this error.

@desk-pro
Copy link

@Fleker Same for me

@Padreco-lbr
Copy link

Padreco-lbr commented May 3, 2023

This problem is still happening, downgrading to versions 11.17.0 or 11.22.0 or latest is currently not working for us, can someone please check this? Its a development blocker since deployment is impossible AND emulators won't start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet