firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Firebase Function deployment fails with error "Build failed: npm ERR! code ERESOLVE"

Open krishna-fenixwork opened this issue 4 years ago • 16 comments

[REQUIRED] Environment info

"firebase": "^9.9.0", "firebase-admin": "^10.0.2", "firebase-functions": "^3.16.0",

Platform: Windows

Step #1: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
Step #1: ○  (Static)  automatically rendered as static HTML (uses no initial props)
Step #1: 
Step #1: ✔  functions: Finished running predeploy script.
Step #1: i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
Step #1: i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
Step #1: i  functions: ensuring required API artifactregistry.googleapis.com is enabled...
Step #1: ✔  functions: required API cloudfunctions.googleapis.com is enabled
Step #1: ✔  functions: required API cloudbuild.googleapis.com is enabled
Step #1: ✔  functions: required API artifactregistry.googleapis.com is enabled
Step #1: i  functions: preparing . directory for uploading...
Step #1: i  functions: packaged . (14.46 MB) for uploading
Step #1: ✔  functions: . folder uploaded successfully
Step #1: i  functions: updating Node.js 16 function nextServer(us-central1)...
.
Step #1: 
Step #1: Functions deploy had errors with the following functions:
Step #1: 	nextServer(us-central1)
Step #1: i  functions: cleaning up build files...
Step #1: ⚠  functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/artifacts/docker/my-project/us-central1/gcf-artifacts
Step #1: 
Step #1: Error: There was an error deploying functions
Finished Step #1
ERROR
ERROR: build step 1 "gcr.io/my-project/firebase" failed: step exited with non-zero status: 2

I get this above error every time I try to deploy my next js project using cloud build. I checked the function logs on cloud functions console as well as on the firebase console and here what it is showing -

Deployment failure:
Build failed: npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: @mui/[email protected]
npm ERR! Found: @types/[email protected]
npm ERR! node_modules/@types/react
npm ERR! dev @types/react@"18.0.15" from the root project
npm ERR! peerOptional @types/react@"^17.0.0 || ^18.0.0" from @mui/[email protected]
npm ERR! node_modules/@mui/base
npm ERR! @mui/base@"5.0.0-alpha.92" from @mui/[email protected]
npm ERR! node_modules/@mui/lab
npm ERR! @mui/lab@"^5.0.0-alpha.93" from the root project
npm ERR! @mui/base@"5.0.0-alpha.92" from @mui/[email protected]
npm ERR! node_modules/@mui/material
npm ERR! @mui/material@"^5.9.3" from the root project
npm ERR! 2 more (@mui/icons-material, @mui/lab)
npm ERR! 9 more (@mui/icons-material, @mui/lab, @mui/material, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peerOptional @types/react@"^17.0.0" from @mui/[email protected]
npm ERR! node_modules/@mui/styles
npm ERR! @mui/styles@"^5.9.3" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: @types/[email protected]
npm ERR! node_modules/@types/react
npm ERR! peerOptional @types/react@"^17.0.0" from @mui/[email protected]
npm ERR! node_modules/@mui/styles
npm ERR! @mui/styles@"^5.9.3" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See /www-data-home/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR! /www-data-home/.npm/_logs/2022-08-03T12_42_24_502Z-debug-0.log; Error ID: beaf8772

At first, I thought it is giving the deployment error because of the material UI library dependency issue. I've updated the libraries to their latest version, but still, the deployment fails.

[REQUIRED] Steps to reproduce

I'm just deploying my next js application using cloud build.

[REQUIRED] Expected behavior

Deployment should happen successfully with no errors

[REQUIRED] Actual behavior

Deployment fails

This is what my package.json contains -

"firebase": "^9.9.0",
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.16.0",
"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.93",
"@mui/material": "^5.9.3",
"@mui/styles": "^5.9.3",
"next": "12.2.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-firebaseui": "^6.0.0"

I've searched a lot about this artifacts files and also manually deleted them from the cloud console. I'm not able to understand what causing this deployment issue?

Can someone help me with that? How we can fix it?

krishna-fenixwork avatar Aug 03 '22 13:08 krishna-fenixwork

This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Aug 03 '22 13:08 google-oss-bot

Hi @krishna-fenixwork thanks for reporting the issue.

functions: Unhandled error cleaning up build images. is unrelated to the issue you are experiencing. Firebase CLI will attempt to delete container images that Google Cloud Function produces as part of function deployment to help save unnecessary storage cost for your containers. It is done in "best effort" basis - it's failure does not indicate your function deployment failed.

From my quick reading of your error message, it sounds like GCF is unable to build your application due to conflicting dependencies. Can you try running the following commands locally?

# This is what Google Cloud Functions will invoke as part of function deplyoment
$ npm ci
# You might have luck fixing dependencies by running the following command
$ npm i

At the end of the day, if you have incompatible dependencies on your project, you may need some manual wrangling to get them fixed before trying to deploy them to Firebase Functions.

taeold avatar Aug 03 '22 17:08 taeold

Hi, @taeold Thanks for replying to the issue. As you said, I've tried running the command locally that you've shared.

This is for npm ci

C:\Users\krish\Downloads\project\my-project>npm ci
npm WARN deprecated [email protected]: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.

added 1035 packages, and audited 1036 packages in 1m

102 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

This is for npm i

C:\Users\krish\Downloads\project\my-project>npm install

up to date, audited 1036 packages in 2s

102 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

As it states, it found 0 vulnerabilities on npm i. I tried again deploying the project but had no luck, the issue persists.

I've one more question, I'm checking on the cloud console, it shows the details for the deployment that I tried on Aug 1 with version 35. How can I check the details of the latest version that I just tried? Or is there any other way, where I can find the issue in detail?

image

krishna-fenixwork avatar Aug 04 '22 04:08 krishna-fenixwork

facing the same issue

UPDATE: Just started working for me now.

RazaShehryar avatar Aug 04 '22 04:08 RazaShehryar

facing the same issue

UPDATE: Just started working for me now.

With no changes? Facing the same issue 🤔

LaithAzzam avatar Aug 04 '22 04:08 LaithAzzam

facing the same issue UPDATE: Just started working for me now.

With no changes? Facing the same issue 🤔

Yeah, I didn't change anything. I guess it was an issue with Cloud Build. I tried deploying one single function and it got deployed successfully.

RazaShehryar avatar Aug 04 '22 04:08 RazaShehryar

Looks like the GOOGLE_RUNTIME_VERSION has been upgraded to 16.16.0 - can confirm the last build to succeed was running 16.15.0 - and if I try to run npm i in my functions directory on node 16.16.0, I'm getting the same error.

LaithAzzam avatar Aug 04 '22 05:08 LaithAzzam

Hi, there. I was right about my instinct for dependency conflicts. So, I've gone through the cloud console logs https://console.cloud.google.com/logs for my function and there also got the same dependency conflict logs which cause the issue in deployment.

Here are the libraries which cause the dependency conflicts in my project -

"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.93",
"@mui/material": "^5.9.3",
"@mui/styles": "^5.9.3",
"react-chartjs-2": "^3.3.0",
"@ant-design/icons": "^4.7.0",

So, I've commented out the code where these libraries were used & also removed them from the package JSON file. Then, I did the npm i and tried the deployment again, and voila it worked!!!! Deployment is successful now. Although, the message regarding the 'Unhandled error cleaning up build images.' still shows on the deployment logs.

Step #1: 
Step #1: ✔  functions: Finished running predeploy script.
Step #1: i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
Step #1: i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
Step #1: i  functions: ensuring required API artifactregistry.googleapis.com is enabled...
Step #1: ✔  functions: required API artifactregistry.googleapis.com is enabled
Step #1: ✔  functions: required API cloudbuild.googleapis.com is enabled
Step #1: ✔  functions: required API cloudfunctions.googleapis.com is enabled
Step #1: i  functions: preparing . directory for uploading...
Step #1: i  functions: packaged . (14.16 MB) for uploading
Step #1: ✔  functions: . folder uploaded successfully
Step #1: i  functions: updating Node.js 16 function nextServer(us-central1)...
Step #1: ✔  functions[nextServer(us-central1)] Successful update operation.
Step #1: Function URL (nextServer(us-central1)): https://us-central1-myproject-dev.cloudfunctions.net/nextServer
Step #1: i  functions: cleaning up build files...
Step #1: ⚠  functions: Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. You can attempt to delete these images by redeploying or you can delete them manually at https://console.cloud.google.com/artifacts/docker/myproject-dev/us-central1/gcf-artifacts
Step #1: 
Step #1: ✔  Deploy complete!
Step #1: 
Step #1: Project Console: https://console.firebase.google.com/project/myproject-dev/overview
Finished Step #1
PUSH
DONE

So, only @mui/styles, react-chartjs-2, @ant-design/icons packages causes the issue. So, I removed them. Here they mentioned that @mui/styles are not compatible with React v18.

https://stackoverflow.com/questions/72997934/dependencies-issues-when-installing-mui/72998258

krishna-fenixwork avatar Aug 04 '22 05:08 krishna-fenixwork

Thanks for the clue @LaithAzzam - it looks like recent version of npm (packaged with node) included a "bugfix" that's now stricter on invalid dependency graph present in your package-lock.json. So npm ci could suddenly start failing w/ the latest version of node 16.

https://github.com/npm/cli/issues/5113

Unfortunately, there isn't a way that I know to pin the version of nodejs16 used on Google Cloud Functions platform (in a way, that is one of the benefit of using GCF - that any security patches/minor updates are automatically applied to your application).

I'm glad to see @krishna-fenixwork originally reported issue be fixed.

Note to others - Sudden appearance of Build failed: npm ERR! code ERESOLVE on your function deployment means your package-lock.json file had latent "bug" i.e. incompatible dependency graph that is now being surfaced. GCF platform recently upgraded to using node 16.16.0 which bundles npm binary that errors out when given invalid dep graph. Please try installing this version of node and run npm ci to reproduce the error locally.

npm core team isn't pursuing a plan to revert this change and recommend users to fix the conflicting dependencies. You can also try using the --legacy-peer-deps flag on npm install command to override the conflicting deps.

taeold avatar Aug 04 '22 12:08 taeold

@taeold 🙏 Anytime! Ty for the response and explanation!

For anyone else that may stumble on this, a lot of our dependency issues were stemming from our upgrade to React 17.

Downgrading to React 16.13.0 cleaned up most of the errors, eventually getting us to successful builds with the fewest additional upgrade / downgrades.

This change is probably better for the long run, given we kept de-prioritizing managing our packages 🙃😅

LaithAzzam avatar Aug 04 '22 16:08 LaithAzzam

@LaithAzzam Thanks for sharing your experience.

I'm going to mark the issue as closed as it seems like there isn't something that we can do to help fix the depedency issues in the CLI.

If anyone have issues or need help, feel free to open up a new issue.

taeold avatar Aug 04 '22 17:08 taeold

What I did was add .npmrc file with content

legacy-peer-deps=true

Reference: issues/241076152

Ridermansb avatar Feb 15 '23 09:02 Ridermansb

I'm faced with a similar error.

I'm referencing a library in the same repo in my package.json as follows:

"dependencies": {
    "@MYLIBRARY/shared": "file:../../shared",

Trying to deploy firebase functions yields

...
Build failed: npm ERR! code EUSAGE
...
npm ERR! Invalid: lock file's @MYLIBRARY/[email protected] does not satisfy @MYLIBRARY/shared@
...

Adding legacy-peer-deps to my .npmrc before npm install didn't seem to help.

I found a workaround (though not a proper fix):

Doesn't work:

npm install
firebase deploy

Works:

npm install
rm package-lock.json
firebase deploy

pyrytakala avatar Feb 18 '23 06:02 pyrytakala

Sadly facing the same issue July 18, 2023. Deleting the package-lock.json feels wrong.

AlexNolasco avatar Jul 18 '23 23:07 AlexNolasco

Can confirm the same behaviour as @pyrytakala . I'm referencing a local library in package.json and when trying a deploy receive the npm ci error.

By removing the package-lock.json (which isn't a long-term fix) I can successfully deploy.

kisamoto avatar Aug 05 '23 09:08 kisamoto

Removing package-lock.json works for me!

alvarocastillop avatar Nov 13 '23 12:11 alvarocastillop