GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.3k stars 160 forks source link

Deploying Functions Framework to GCF Fails with /bin/bash: node_modules/.bin/functions-framework: No such file or directory #431

Closed Crash-GHaun closed 2 years ago

Crash-GHaun commented 2 years ago

Hey Functions Framework Team!

I was attempting to deploy an Node JS (ESM) function to Cloud Functions when I hit a bit of a bug. When the function deploys on GCF I see the following error in the logs: /bin/bash: node_modules/.bin/functions-framework: No such file or directory

When running the function locally all works as intended.

Package.json:

{
    "type": "module",
    "name": "test_bot",
    "version": "0.1.0",
    "description": "Test Bot",
    "scripts": {
        "start": "npx functions-framework --target=test_bot"
    },
    "dependencies": {
        "@google-cloud/functions-framework": "^3.0.0",
        "@google-cloud/secret-manager": "^3.10.1"
    },
    "devDependencies": {
        "@google-cloud/functions-framework": "^3.0.0"
    }
}

index.js:

export const test_bot = async (req, res) =>  {
  res.send("Hello World");
};

To deploy: gcloud beta functions deploy functions_test --region=europe-west1 --trigger-http --runtime=nodejs16

Things work locally when you run npm start but fails when it's on GCF. Adding --entry-point to the deployment makes no difference.

Do we need a different start script for GCF?

grant commented 2 years ago

Hey @Crash-GHaun,

Thanks for the issue. Debugging this, I found that having the FF in both deps and devDeps causes errors in Cloud Build when building the function.

You don't need @google-cloud/functions-framework in both. Just one or the other. GCF adds it if you don't have an explicit dep, so I would add it there. If you install via npm, you cannot produce a package.json like that (at least normally).

This worked for me:

{
    "type": "module",
    "name": "test_bot",
    "version": "0.1.0",
    "description": "Test Bot",
    "scripts": {
        "start": "npx functions-framework --target=test_bot"
    },
    "dependencies": {
        "@google-cloud/functions-framework": "^3.0.0",
        "@google-cloud/secret-manager": "^3.10.1"
    }
}
gcloud functions deploy functions_test --trigger-http --runtime nodejs16 --entry-point=test_bot

Maybe @matthewrobertson knows what Buildpacks currently does to attempt to resolve this, if this is a bug, and maybe how we could resolve interesting error / edge case.

Crash-GHaun commented 2 years ago

Can confirm this solved my problem! Thank you!

grant commented 2 years ago

You're welcome. Have a good one :)


Closing this issue, as this is a misconfig / unsupported use of npm package.json.

Docs: https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file

sadrayan commented 1 year ago

wow... that was a bizzare issue... solved it for cloudfunction v2