部署Firebase函数遇Cannot find module 'firebase-functions'错误求助
Let’s work through this issue step by step—since you’ve already tried the basic npm install and still hit this error, we need to dig into a few less obvious fixes:
1. Upgrade Your Node.js Version
Your current Node.js version (v7.0.0) is extremely outdated. Firebase Functions requires at least Node.js v14 (LTS versions like v18 or v20 are recommended now). Older Node versions can cause npm to fail installing modern dependencies correctly, and some packages (including firebase-functions) might not even support such old versions.
- Install a recent LTS version via nvm (the easiest way to manage Node versions) or the official Node.js website.
- After upgrading, restart your terminal to ensure the new Node/npm version is active.
2. Do a Clean Dependency Install
Cached files or incomplete installs can leave dependencies in a broken state. Let’s reset everything:
- Navigate to your functions directory:
cd /Users/user/Documents/Projects/firebase/hnpwa-firebase/functions - Delete the existing
node_modulesfolder and lock file:
(For Windows, userm -rf node_modules package-lock.jsonrmdir /s node_modulesanddel package-lock.json) - Clear npm’s cache to eliminate any corrupted cached packages:
npm cache clean --force - Reinstall dependencies from scratch:
npm install
3. Check firebase-functions is in Dependencies
Double-check your package.json in the functions directory to make sure firebase-functions is listed under dependencies (not devDependencies). Firebase only installs packages in dependencies during deployment.
You should see something like this:
"dependencies": { "firebase-functions": "^4.0.0", // other dependencies... }
If it’s in devDependencies, move it to dependencies and re-run npm install.
4. Verify Firebase Deployment Configuration
Your deployment targets the dist/server directory—make sure your firebase.json is set up correctly to point to the right source and run the build script properly. It should look similar to this:
{ "functions": { "source": "functions", "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run build" ] } }
This ensures the TypeScript code is compiled to dist before deployment, and Firebase can locate the compiled files with the correct dependencies.
5. Upgrade Firebase CLI
Outdated Firebase CLI versions can cause compatibility issues with newer firebase-functions versions. Upgrade it globally:
npm install -g firebase-tools
After trying these steps, attempt to deploy again with firebase deploy --only functions. This should resolve the module not found error.
内容的提问来源于stack exchange,提问作者Pyae Phyoe Shein

