TypeError: Cannot read property 'wanted' of undefined:

TypeError: Cannot read property 'wanted' of undefined:

我一整天都在使用 firebase 成功部署功能,学习如何使用它。我试图看看如果我初始化另一个部署到同一个项目的目录会发生什么,并且在我更新我的 npm 版本之前没有任何问题,现在我每次尝试部署时都会得到 "Unexpected error has occurred"

我尝试通过让我自己的用户成为 node_modules、bin 和 share 目录的所有者来更新 npm 权限。我尝试卸载并重新安装 firebase-tools。我还尝试删除所有当前函数目录并初始化一个新目录并重新安装我的依赖项。

这是调试日志

Dylans-MacBook-Pro-3:functions dsenderling$ firebase deploy --debug
[2019-07-03T18:04:35.526Z] ----------------------------------------------------------------------
[2019-07-03T18:04:35.528Z] Command:       /usr/local/bin/node /usr/local/bin/firebase deploy --debug
[2019-07-03T18:04:35.529Z] CLI Version:   7.0.2
[2019-07-03T18:04:35.529Z] Platform:      darwin
[2019-07-03T18:04:35.529Z] Node Version:  v10.16.0
[2019-07-03T18:04:35.529Z] Time:          Wed Jul 03 2019 13:04:35 GMT-0500 (Central Daylight Time)
[2019-07-03T18:04:35.529Z] ----------------------------------------------------------------------

[2019-07-03T18:04:35.537Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2019-07-03T18:04:35.537Z] > authorizing via signed-in user
[2019-07-03T18:04:35.537Z] [iam] checking project my-awesome-project-5a4e9 for permissions ["cloudfunctions.functions.create","cloudfunctions.functions.delete","cloudfunctions.functions.get","cloudfunctions.functions.list","cloudfunctions.functions.update","cloudfunctions.operations.get","firebase.projects.get"]
[2019-07-03T18:04:35.539Z] >>> HTTP REQUEST POST https://cloudresourcemanager.googleapis.com/v1/projects/my-awesome-project-5a4e9:testIamPermissions  
 permissions=[cloudfunctions.functions.create, cloudfunctions.functions.delete, cloudfunctions.functions.get, cloudfunctions.functions.list, cloudfunctions.functions.update, cloudfunctions.operations.get, firebase.projects.get]
[2019-07-03T18:04:35.769Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Wed, 03 Jul 2019 18:04:35 GMT, server=ESF, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, server-timing=gfet4t7; dur=83, alt-svc=quic=":443"; ma=2592000; v="46,43,39", accept-ranges=none, transfer-encoding=chunked
[2019-07-03T18:04:37.033Z] TypeError: Cannot read property 'wanted' of undefined
    at /usr/local/lib/node_modules/firebase-tools/lib/checkFirebaseSDKVersion.js:37:51
    at process._tickCallback (internal/process/next_tick.js:68:7)

Error: An unexpected error has occurred.

我的直觉告诉我 firebase-tools 或我的 firebase sdk 有问题,但我不知道是什么。提前致谢

这也开始发生在我身上......

看起来 npm 正在为此命令输出不同的结果

npm outdated firebase-functions --json=true
// for me outputs  {}\n

脚本 checkFirebaseSDKVersion.js 期待这样的结果(如果您的 firebase-functions 实际上已经过时,它就会得到)

{
  "current": "2.5.0",
  "wanted": "2.5.0",
  "latest": "3.0.2",
  "location": "node_modules/some path /firebase-functions"
}

或者空白输出...更可能是你的情况

你可以'Fix'做什么

当它开始影响更多人时,它可能会很快得到修复... 目前 调整 /usr/local/lib/node_modules/firebase-tools/lib/checkFirebaseSDKVersion.js

添加此内容以说明第 24 行

附近 {}\n 的更新空输出
            if (data && data.toString() !== "{}\n") {
                output = JSON.parse(data.toString("utf8")); // existing Code!
            }

不确定 npm 的更新过程是如何工作的,因此您可能必须还原它以在修复后更新它,但我不这么认为。

希望对您有所帮助!

我认为这个问题是由 npm 6.10.0 中的修复引起的,请参阅 https://github.com/npm/cli/pull/176

解决方法是修改 /usr/lib/node_modules/firebase-tools/lib/checkFirebaseSDKVersion.js (linux)。 对于 macOS 和 nvm,请参阅下面的评论。

来自:

if (!output) {
  return;
}

至:

if (!output || !output["firebase-functions"]) {
  return;
}

有完全相同的问题,在将 npm 从 6.9.2 更新到 6.10.0 后立即启动。

最终降级回 6.9.2 (npm install -g npm@6.9.2),我的 firebase 部署马上又开始工作了。

编辑:firebase 部署正在使用 npm 6.10.1,现在可以安全更新了!

npm@6.12.0 和 firebase-functions@3.3.0 有同样的问题。

TypeError: Cannot read property 'wanted' of undefined
    at /usr/local/lib/node_modules/firebase-tools/lib/checkFirebaseSDKVersion.js:37:51
    at process._tickCallback (internal/process/next_tick.js:68:7)

已通过更改 /usr/local/lib/node_modules/firebase-tools/lib/checkFirebaseSDKVersion.js 第 37 行修复。

var wanted = (output["firebase-functions"] || {}).wanted;
var latest = (output["firebase-functions"] || {}).latest;

尝试:

npm i -g firebase-tools@latest

由于 mac 上的旧版本 Firebase CLI,我遇到了同样的问题,尝试使用标准命令 npm 命令进行更新 ==> npm install -g firebase-tools 没有帮助,因为它表明我的CLI 已更新,但部署功能命令选择了一个旧的缓存版本,所以对我有用的是使用 自动安装脚本 升级到最新版本:-

curl -sL firebase.tools | upgrade=true bash

更多细节可以参考官方文档here