【问题标题】:Error: Cannot find module 'google-protobuf'错误:找不到模块“google-protobuf”
【发布时间】:2016-03-23 08:50:04
【问题描述】:

继我之前的question 之后,我正在尝试在 node.js 中使用协议缓冲区。我从ServiceMessage.proto 生成了ServiceMessage_pb.js,并添加了以下代码:

var messages = require('./ServiceMessage_pb');

现在我的节点日志中出现以下错误:

Error: Cannot find module 'google-protobuf'
    at Function.Module._resolveFilename (module.js:325:15)
    at Function.Module._load (module.js:276:25)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/aii/ws/ServiceMessage_pb.js:8:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)

有什么建议可以解决这个问题吗?

谢谢

【问题讨论】:

  • npm.org 没有列出任何名为 google-protobuf 的模块。可能它已被移动、重命名或在其他地方发布。
  • ServiceMessage_pb.js 的第 8 行有一条 require 语句,特别需要 google-protobuf。看起来这个文件的生成器使用了 google-protobuf,而实际的模块是 protobuf。

标签: javascript node.js protocol-buffers proto


【解决方案1】:

您可以使用 npm 安装模块:

npm install --save protobuf

然后这样要求:

var my_protobuff = require ("protobuf");

【讨论】:

  • 你能看看我的上一个问题吗(上面的链接)-我已经下载了源代码,编译并安装了。我认为我不应该再做一次。我会尝试你关于需求的第二个建议并很快更新.. 10x
  • 你不需要自己编译源代码。只需使用 npm
  • 能把你项目的目录结构贴一下吗?
  • 我认为这一切都不稳定,因为我使用的是 v3.0.0 beta...我会尝试降级到 2.6 - 无论如何谢谢..
  • WARNING 这不是官方的谷歌包。查看其他答案。
【解决方案2】:
    npm i google-protobuf

我在安装“protobuf”时遇到了很多问题!

官方的 protobuf javascript 包也是“google-protobuf”: https://www.npmjs.com/package/google-protobuf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多