FIrebase deploy error: Cannot find module 'firebase-admin'

FIrebase deploy error: Cannot find module 'firebase-admin'

我最近在尝试部署到 Firebase 时开始收到以下错误(在过去成功部署之后)。我不确定发生了什么变化才能开始发生这种情况。如果我 运行 firebase 服务于本地主机,一切正常。我的 package.json 和 index.js 的要求也在下面。

i  deploying functions, hosting
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
+  runtimeconfig: all necessary APIs are enabled
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (46.02 KB) for uploading
+  functions: functions folder uploaded successfully
i  hosting: preparing public directory for upload...
!  Warning: Public directory does not contain index.html
+  hosting: 9 files uploaded successfully
i  starting release process (may take several minutes)...
i  functions: updating function app...
!  functions[app]: Deploy Error: Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'firebase-admin'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/apps.j
s:25:16)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)


Functions deploy had errors. To continue deploying other features (such as datab
ase), run:
firebase deploy --except functions

Error: Functions did not deploy properly.

package.json:

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"dependencies": {
"@google-cloud/vision": "^0.12.0",
"async": "^2.5.0",
"consolidate": "^0.14.5",
"express": "^4.15.4",
"firebase-admin": "^5.4.0",
"firebase-functions": "^0.5.7",
"handlebars": "^4.0.10",
"jquery": "^3.2.1",
"js-levenshtein": "^1.1.3",
"json-query": "^2.2.2"
},
"private": true
}

来自index.js:

const functions = require('firebase-functions');
const firebase = require('firebase-admin');
const express = require('express');
const engines = require('consolidate');
const Vision = require('@google-cloud/vision');
const levenshtein = require('js-levenshtein');
const restName = require('./restName');
const parser = require('./parser');
const jsonQuery = require('json-query')

const firebaseApp = firebase.initializeApp(
functions.config().firebase
);

他们有一个活跃的服务中断。按照此解决方法:https://status.firebase.google.com/incident/Functions/17024

运行 函数存储库中的以下命令:

npm install --save-exact firebase-functions@0.7.0 npm install --save-exact firebase-admin@5.4.0

然后再次尝试部署函数:

firebase deploy --only functions

如果 npm 不起作用,您可以尝试对我们有用的纱线:

yarn add firebase-functions@0.7.0 --exact yarn add firebase-admin@5.4.0 --exact

更新文件夹函数中的文件 package.json 并查看依赖项 从 firebase-admin 中删除符号 ~ 并从 firebase-functions 中删除 ^ 它应该像:

"dependencies": {
  "firebase-admin": "5.4.0",
  "firebase-functions": "0.7.0"
}

在命令行输入

npm install

然后再次尝试部署。

我也遇到了同样的错误。我修复了在 nodejs 中安装不同版本的错误。我卸载了我的新版本并安装了 Node v7.8.0。然后它工作正常:)

我去了函数目录和 运行 yarn(或者 npm install 如果你是老派)。然后回到我的项目目录,再次运行 firebase deploy

所以问题是 Functions 文件夹中的 package.json 与项目目录中的 package.json 不同.是的,您有 2 个此文件!

所以如果你想部署,(deploy Functions),运行 npm install express 或在 Function 文件夹中安装其他依赖项,你将能够正确部署。

尝试安装:npm i -g firebase-tools@6.8.0

这解决了我的问题。

运行以下命令

npm i firebase-admin 

如果使用 docker,请确保您本地的 nodejs 版本与您在 docker 文件中使用的版本相同。 例如,如果我使用的是节点 v16.14.0,(通过在您的终端中输入它来查找)node -v,那么样本 docker 文件应该是:

FROM node:16.14.0-alpine                             
ENV NODE_ENV=production                 
WORKDIR /app                            
COPY package*.json ./                  
RUN npm install --production            
COPY . .                                
CMD [ "npm", "start" ] 

             

我为解决这个问题所做的事情

1.) 确保我在正确的目录中 package.json

2.) rm -rf node-modules

3.) npm i (missing module)

4.) 使缺少的依赖项在 devDependenciesNOT 而在 package.json 中的 dependencies 中,在 devDependencies 得到部署