firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 912 forks source link

Next.js cloud function builds failing on first firebase deploy #6446

Open christopherdbull opened 8 months ago

christopherdbull commented 8 months ago

[REQUIRED] Environment info

firebase-tools: 12.7.0

Platform: macOS

Next: 13.4.19

[REQUIRED] Test case

Added firebase to an existing, simple next app with a couple of api endpoints and the cloud function build fails.

[REQUIRED] Steps to reproduce

Add firebase with firebase hosting init to a next.js application with server side endpoints. run firebase deploy

[REQUIRED] Expected behavior

The functions build finishes

[REQUIRED] Actual behavior

Building a Cloud Function to run this application. This is needed due to:
 • Image Optimization
 • non-static component /api/hubspot/route
 • non-static component /api/getstarted/route
 • non-static component /api/superblocks/route

npm ERR! code 2
npm ERR! path /Users/christopherbull/dev/1z_web/one-zero/.firebase/website-399615/functions/node_modules/sharp
npm ERR! command failed
npm ERR! command /Users/christopherbull/.cache/firebase/runtime/shell -c -- (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! /bin/sh: --: invalid option
npm ERR! Usage: /bin/sh [GNU long option] [option] ...
npm ERR!    /bin/sh [GNU long option] [option] script-file ...
npm ERR! GNU long options:
npm ERR!    --debug
npm ERR!    --debugger
npm ERR!    --dump-po-strings
npm ERR!    --dump-strings
npm ERR!    --help
npm ERR!    --init-file
npm ERR!    --login
npm ERR!    --noediting
npm ERR!    --noprofile
npm ERR!    --norc
npm ERR!    --posix
npm ERR!    --protected
npm ERR!    --rcfile
npm ERR!    --restricted
npm ERR!    --verbose
npm ERR!    --version
npm ERR!    --wordexp
npm ERR! Shell options:
npm ERR!    -irsD or -c command or -O shopt_option      (invocation only)
npm ERR!    -abefhkmnptuvxBCHP or -o option

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/christopherbull/.npm/_logs/2023-10-13T11_12_35_546Z-debug-0.log
[2023-10-13T11:12:45.468Z] Error: Command failed: npm i --omit dev --no-audit
    at checkExecSyncError (node:child_process:828:11)
    at Object.execSync (node:child_process:899:15)
    at execSync (pkg/prelude/bootstrap.js:2111:30)
    at prepareFrameworks (/Users/christopherbull/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/frameworks/index.js:367:42)
    at async deploy (/Users/christopherbull/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/index.js:55:13)
cmrqs commented 8 months ago

I would just like to add that following this guide also leads to the same problem.

MaRaSu commented 8 months ago

I'm on Mac too, Next 13.4.1 and was on firebase tools 12.4.4 until just updated to 12.7.0 to check if it would fix this issue - it did not. Up until yesterday evening my app had been deploying for months nicely to Firebase framework-aware hosting for Next JS. I made today a couple of small code changes - no new libs, no new config, nothing major like that. And suddenly firebase deploy fails in build step with similar symptoms: sharp & libvips.

Checked manually on my Mac to download the sharp-libvips outlined in the error message. Downloaded nicely.

i  functions: updating Node.js 18 (2nd Gen) function firebase-frameworks-trailmapv2-staging:ssrtrailmapv2staging(europe-west1)...
.....
npm ERR! code 1
npm ERR! path /workspace/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! sharp: Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.14.5/libvips-8.14.5-linux-x64.tar.br
npm ERR! sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
npm ERR! sharp: Installation error: read ECONNRESET

npm ERR! A complete log of this run can be found in: /www-data-home/.npm/_logs/2023-10-22T14_18_41_613Z-debug-0.log.
MaRaSu commented 8 months ago

...Tried again to deploy this morning. The issue I reported above 15 hours ago is gone - luckily a temporary Cloud Build issue.

ertugrulaktepe commented 8 months ago

Can anyone solve it?

`Building a Cloud Function to run this application. This is needed due to: • non-static component /_not-found

npm ERR! code 2 npm ERR! path /Users//Desktop//.firebase/function-name/functions/node_modules/protobufjs npm ERR! command failed npm ERR! command /Users/**/.cache/firebase/runtime/shell -c -- node scripts/postinstall npm ERR! /bin/sh: --: invalid option npm ERR! Usage: /bin/sh [GNU long option] [option] ... npm ERR! /bin/sh [GNU long option] [option] script-file ... npm ERR! GNU long options: npm ERR! --debug npm ERR! --debugger npm ERR! --dump-po-strings npm ERR! --dump-strings npm ERR! --help npm ERR! --init-file npm ERR! --login npm ERR! --noediting npm ERR! --noprofile npm ERR! --norc npm ERR! --posix npm ERR! --protected npm ERR! --rcfile npm ERR! --restricted npm ERR! --verbose npm ERR! --version npm ERR! --wordexp npm ERR! Shell options: npm ERR! -irsD or -c command or -O shopt_option (invocation only) npm ERR! -abefhkmnptuvxBCHP or -o option

npm ERR! A complete log of this run can be found in: npm ERR! /Users/**/.npm/_logs/2023-10-24T11_44_03_007Z-debug-0.log

Error: An unexpected error has occurred.`

ginagr commented 7 months ago

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.
miloops commented 7 months ago

Same issue on fresh firebase init and deploy on Mac

ginagr commented 7 months ago

Please let me know if there are any other logs or info I can send along to help debug - this is a pretty big issue for my company as we cannot deploy updates to our production firebase functions

FantixX commented 7 months ago

Same issue also on MacOS Sonoma


Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2```
IAmKio commented 7 months ago

Also having the above issue. Clean install, new project, nothing untoward going on. Firebase Functions default code, typescript, firebase-cli@12.9.1

ginagr commented 7 months ago

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I was able to deploy by downgrading my firebase tools npm -g install firebase-tools@12.0.0

german1311 commented 7 months ago

This just happened to me on an M1 Mac, I removed my current version npm -g uninstall firebase-tools or sudo rm -rf /usr/local/bin/firebase // only if you have installed the CLI in a different way

and reinstalled the old firebase-tools@12.0.0 did the trick

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I was able to deploy by downgrading my firebase tools npm -g install firebase-tools@12.0.0

samuel-coapps commented 6 months ago

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I was able to deploy by downgrading my firebase tools npm -g install firebase-tools@12.0.0

Using firebase-tools@12.1.0 can provide a workaround if you need to deploy to nodejs20.

StephanWalters commented 6 months ago

thank

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I was able to deploy by downgrading my firebase tools npm -g install firebase-tools@12.0.0

Using firebase-tools@12.1.0 can provide a workaround if you need to deploy to nodejs20.

thank you! I just had a WTF moment. I can confirm uninstalling and downgrading to 12.0.0 did the trick.

JulienMartel commented 6 months ago

It seems like there's multiple issues here, although the root of the issues may be the use of npm --prefix flag with different npm versions. I wasn't able to reproduce this locally. Here are my versions:

To help diagnose further, I'd appreciate if others still seeing this problem could provide additional logs and details like the versions listed above.

rzfang commented 6 months ago

@JulienMartel I still have the same issue.

My env:

rzfang commented 6 months ago

I used firebase-tools@13.0.1 by installed shell command. Just tried @13.0.2 installed by npm under my project directly, and deploy successfully.

@JulienMartel I still have the same issue.

My env:

  • next: 14.0.4
  • firebase-tools: 13.0.1
  • npm: 10.2.3
  • node: v20.10.0
  • macOS: Sonoma 14.2 (M1)
zarko-tg commented 5 months ago

Same issue here for a pure JS function that's been deployed before without any issues. Env:

zarko-tg commented 5 months ago

FYI, my current workaround is removing

"predeploy": [
  "npm --prefix \"$RESOURCE_DIR\" run lint"
],

from firebase.json.

That being done the deployment works, but be aware that you lose the automatic linting on deploy. I suppose it might even be a more permanent workaround to pre-chain the linting in the function's package.json on deploy.

leoortizz commented 5 months ago

Per the comments and error logs this issue doesn't seem related to the frameworks integration but rather to the Firebase Functions

dumitruPuggle commented 5 months ago

FYI, my current workaround is removing

"predeploy": [
  "npm --prefix \"$RESOURCE_DIR\" run lint"
],

from firebase.json.

That being done the deployment works, but be aware that you lose the automatic linting on deploy. I supposed it might even be a more permanent workaround to pre-chain the linting in the function's package.json on deploy.

Thank you very much!

gmotzespina commented 5 months ago

Same issue here.

I was using node 16. It worked just by switching to node v20.5.1 (npm v10.0.0).

cobrakai-lab commented 5 months ago

This help me fix the issue without workarounds: I reinstalled firebase-cli using npm install -g firebase-tools I believe previous I installed using curl -sL https://firebase.tools | bash

estevan-ulian commented 4 months ago

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I'm having the same problem deploying my functions. I've tried downgrading to the versions mentioned above, reinstalling, removing the lint command line in the firebase.json predeploy, and nothing... I'm trying to do it from macOS Sonoma. When I test using emulators, it works normally.

estevan-ulian commented 4 months ago

I am unable to deploy my production firebase functions due to the same error


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

> build
> npm run lint && tsc

/bin/sh: --: invalid option
Usage:  /bin/sh [GNU long option] [option] ...
        /bin/sh [GNU long option] [option] script-file ...
GNU long options:
        --debug
        --debugger
        --dump-po-strings
        --dump-strings
        --help
        --init-file
        --login
        --noediting
        --noprofile
        --norc
        --posix
        --protected
        --rcfile
        --restricted
        --verbose
        --version
        --wordexp
Shell options:
        -irsD or -c command or -O shopt_option          (invocation only)
        -abefhkmnptuvxBCHP or -o option

Error: functions predeploy error: Command terminated with non-zero exit code 2
error Command failed with exit code 1.

I'm having the same problem deploying my functions. I've tried downgrading to the versions mentioned above, reinstalling, removing the lint command line in the firebase.json predeploy, and nothing... I'm trying to do it from macOS Sonoma. When I test using emulators, it works normally.

I managed to solve it by simply installing firebase-tools in my project directory, not globally: cd my-project/functions && npm install firebase-tools

Arthurysh commented 4 months ago

I can confirm that lowering the version of firebase tools helped. sudo rm -rf /usr/local/bin/firebase npm -g install firebase-tools@12.0.0

stardvst commented 4 months ago

i had the same problem. i had installed firebase with curl -sL https://firebase.tools | bash command, and seems that caused the issue. after uninstalling it, and installing firebase via npm, firebase deploy works without any error: curl -sL firebase.tools | uninstall=true bash npm uninstall -g firebase-tools i'm using firebase v13.1.0.

alexodus commented 4 months ago

I managed to solve it by simply installing firebase-tools in my project directory, not globally: cd my-project/functions && npm install firebase-tools

This works for me. Thanks!

vlad-tura commented 4 months ago

On MacOS Sonoma 14.1.1 I fixed it by removing the version installed with curl -sL https://firebase.tools | bash and installing it via brew brew install firebase-cli

HugoGresse commented 4 months ago

new mac, node 20, new install through the bash script, deploy failed for the same reason. Has to use answer from https://github.com/firebase/firebase-tools/issues/6446#issuecomment-1925811867 above and reinstall through npm.

It needs more attention from Firebase team. But at least we have workarounds.

maccman commented 4 months ago

Yes, the version of Firebase Tools installed by curl is completely broken. Needs fixing.

Budyn commented 3 months ago

Problem still exists. Please!

epiphanizer commented 3 months ago

Can confirm using the bash script caused issues for me. Just delete the globally installed package found with which firebase, and run which again, you'll see it pointing to the right place if you installed with npm.

soymartinez commented 3 months ago

This worked for my M1 pro, the problem is in the firebase cli:

[!CAUTION] DO NOT INSTALL: curl -sL https://firebase.tools | bash.

SOLUTION:

  1. Uninstall all firebase cli: curl -sL firebase.tools | uninstall=true bash

  2. Install firebase cli from brew or with npm: brew install firebase-cli | npm install -g firebase-tools

  3. Run your code, example: firebase deploy --only functions

patgmiller commented 3 months ago

Honestly I don't find having a global install the best approach. Especially since I have multiple projects and one would need to be able to manage the different required versions I simply just include firebase as a dependency and everything is solved with npx firebase deploy --only functions

phillbooth commented 1 month ago

Solution firebase.json changes

"hosting": { "public": ".next", "rewrites": [ { "source": "**", "function": "nextServer" }

Hello everyone, My app is next.js v14, firebase, auth and firestore

For me using the latest version of firebase-tools and "#firebase deploy --only" functions were working but when I did a full deploy I got this error.

I have done a "firebase deploy" test and got an error


Building a Cloud Function to run this application. This is needed due to:
 • non-static route /jobs/[name]/[county]/[id]
 • non-static route /search/results
 • non-static route /sitemap/[person]

I took a look at my firebase.json in the root folder of my app and found it needed some changes.

Here is my full firebase.json file, please take a look at the "Hosting" section for the solution

{ "firestore": { "rules": "firestore.rules", "indexes": "firestore.indexes.json" }, "functions": [ { "source": "functions", "codebase": "default", "ignore": [ "node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log", "*.local" ], "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run lint", "npm --prefix \"$RESOURCE_DIR\" run build" ] } ], "hosting": { "public": ".next", "rewrites": [ { "source": "**", "function": "nextServer" } "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], ], "frameworksBackend": { "region": "europe-west1" } }, "storage": { "rules": "storage.rules" }, "extensions": {} }