firebase / firebase-functions

Firebase SDK for Cloud Functions
https://firebase.google.com/docs/functions/
MIT License
1.03k stars 203 forks source link

firebase deploy showing unexpected errors during npm i #1372

Closed ohabash closed 1 year ago

ohabash commented 1 year ago

Related issues

https://github.com/lovell/sharp/issues/3607

Version info

node: => v16.19.0

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "postbuild": "cp -r src/email/emails lib/email",
    "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": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "email-templates": "^9.0.0",
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.1",
    "pug": "^3.0.2",
    "sharp": "^0.32.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "4.9.3"
  },
  "private": true
}

Steps to reproduce

  1. add "sharp": "^0.32.0" to functions/package.json => dependencies.
  2. firebase deploy
  3. no need to use sharp in any file. to see the error

Expected behavior

expected sharp to be installed on host

Actual behavior

npm install is throwing the following error. its curious that the error contains file paths from my local machine. as i was expecting the paths to be from the host server

=== Deploying to 'habitat-commons-cms'...

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

> functions@ build /Users/omarhabash/Sites/habitat_commons_nx/functions
> tsc

> functions@ postbuild /Users/omarhabash/Sites/habitat_commons_nx/functions
> cp -r src/email/emails lib/email

✔  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled

Error: Failed to load function definition from source: Failed to generate manifest from function source: Error:
Something went wrong installing the "sharp" module

Cannot find module '../build/Release/sharp-darwin-x64.node'
Require stack:
- /Users/omarhabash/Sites/habitat_commons_nx/functions/node_modules/sharp/lib/sharp.js
- /Users/omarhabash/Sites/habitat_commons_nx/functions/node_modules/sharp/lib/constructor.js
- /Users/omarhabash/Sites/habitat_commons_nx/functions/node_modules/sharp/lib/index.js
- /Users/omarhabash/Sites/habitat_commons_nx/functions/lib/index.js
- /Users/omarhabash/Sites/habitat_commons_nx/functions/node_modules/firebase-functions/lib/runtime/loader.js
- /Users/omarhabash/Sites/habitat_commons_nx/functions/node_modules/firebase-functions/lib/bin/firebase-functions.js
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.

Possible solutions:
- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
- Install for the current darwin-x64 runtime: "npm install --platform=darwin --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install

Having trouble? Try firebase [command] --help
google-oss-bot commented 1 year ago

I found a few problems with this issue:

blidd-google commented 1 year ago

Hi @ohabash, are your functions set up in an Nx monorepo environment? Unfortunately at this time, Cloud Functions for Firebase does not support monorepo setups, which is likely the reason why the CLI is unable to find the module ../build/Release/sharp-darwin-x64.node. At deploy time, the CLI will zip the functions source directory and upload it to Google Cloud Storage, so if you have any local references to modules located outside of the functions directory, then you will encounter a dependency error.

To help us troubleshoot this issue, could you provide more context about your project's directory structure?

ohabash commented 1 year ago

@blidd-google yes i am using nx.

do you suggest firebase init functions in a new directory so that none of the bigger projects' settings are involved?

blidd-google commented 1 year ago

If you move your functions code into a new directory outside of your Nx monorepo and sharp is included in your package.json dependencies, you should be able to deploy with no issues. I'm going to go ahead and close this issue for now, but if you are still encountering the same problem, feel free to ping me on this thread and I can re-open the issue.

ohabash commented 1 year ago

Thank you for your help. I did move everything into a clean directory and same error. I dont believe NX is a problem as i have several other functions working fine.. My question remains. why are the npm install errors showing paths from my local machine; the error shows a problem with npm install which should be happening on the remote host. @blidd-google

ohabash commented 1 year ago

I dont believe anyone can can use "sharp": "^0.32.0", in their function as all i have done is added it to my functions/package.json and that caused this problem.

Its very easy to reproduce.