问题
I am trying to use Protocol Buffers - Google's data interchange format referring https://github.com/google/protobuf/tree/master/js
I tried to follow documentation i am able to get the setup of Protocol Compiler and able to do
protoc --js_out=library=myproto_libs,binary:. messages.proto
but when i do npm install google-protobuf i am getting
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/google-
npm ERR! 404
npm ERR! 404 'google-protobuf' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
and if i write the sample which can use that generated binary it says goog is not defined
goog.require('com.nec.eva.msap.proto.alerts.FaceAlert');
var message = com.nec.eva.msap.proto.alerts.FaceAlert();
message.watchlist_id("1");
message.candidate_id("25");
message.gender("female");
bytes = message.serializeBinary();
I am not sure why i am not able to do npm install and also how that goog.require('com.nec.eva.msap.proto.alerts.FaceAlert'); will use the generated binary please help i have almost spent a day to understand but not getting it
回答1:
Best to search on http://node-modules.com/search?q=protocol+buffers. This one is popular and should work: https://github.com/dcodeIO/protobuf.js
回答2:
Simply, google-protobuf
is not in the npm repository, so installing it will not work (see https://www.npmjs.com/package/google-protobuf). However, there are a number of protocol buffers libraries that you could try:
- https://www.npmjs.com/package/protocol-buffers this one plays a bit fast and loose, but is really good for many simple cases (and it's blindingly fast).
- https://www.npmjs.com/package/protobufjs aims to be a bit more complete; I haven't personally used this one, but it's well regarded and has a large number of downloads.
回答3:
There are issues and
the API is not well-documented yet
You need to compile it with protoc messages.proto --js_out=import_style=commonjs,binary:.
Or set
var jspb = require('google-protobuf')
var goog = jspb
来源:https://stackoverflow.com/questions/36418217/goog-is-not-defined-error-while-trying-to-use-protocol-buffers-googles-data-i