Here’s what actually fixed it:
Run npm config set legacy-peer-deps false in the terminal.
Delete node_modules and package-lock.json.
In some cases, you may also need to delete functions the firebase and set it up again.
Run firebase deploy --only functions.
Finally, don’t forget to run npm install inside the functions folder before deploying again.
See the reference where I discovered this: https://stackoverflow.com/a/77823349/23242867