Here are the steps I used to try and load a proto file in node.js. Overall, how do I get protocol buffers to work with node.js?
1) Dowloaded and installed protocol buffers into this location.
/home/ubuntu/Downloads/protobuf-2.4.1
2) Downloaded this protobuf-for-node fix from this repo. I places the folder in the node_modules folder of my node.js app folder.
git://github.com/pzgz/protobuf-for-node.git
3) Ran the following command
cd node_modules/protobuf-for-node
PROTOBUF=~/Downloads/protobuf-2.4.1 /usr/local/bin/node-waf configure clean build
4) Moved the following files to these locations:
cp node_modules/protobuf-for-node/protobuf_for_node.h /usr/local/include/node
cp node_modules/protobuf-for-node/build/Release/protobuf_for_node_lib.so /usr/local/lib
5) Ran the following to build the feeds.proto example that ships with node.js
cd node_modules/protobuf-for-node/example
protoc --descriptor_set_out=feeds.desc --include_imports feeds.proto
6) In the app, I added this line.
var express = require('express'), routes = require('./routes');
var fs = require('fs');
var puts = require('util').puts;
url = require('url');
moment = require('moment');
var uuid = require('node-uuid');
var app = module.exports = express.createServer();
var Schema = require('protobuf_for_node').Schema;
var schema = new Schema(fs.readFileSync('/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbNodejsServer/node_modules/protobuf-for-node/example/feeds.desc'));
7) Here is my error.
module.js:337
throw new Error("Cannot find module '" + request + "'");
^
Error: Cannot find module 'protobuf_for_node'
at Function._resolveFilename (module.js:337:11)
at Function._load (module.js:279:25)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (/home/ubuntu/app.js:9:14)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
Consider using this fork of the library instead:
https://github.com/chrisdew/protobuf/
It seems to be more actively maintained and has better documentation. Installing it was npm was trivial and doing a require('protobuf')
is working fine for me.
For Ubuntu I needed to install the following to get the package installed with npm and get the proto file compiled:
sudo apt-get install libprotobuf-dev protobuf-compiler
It looks like you already have those but installed them by hand.
Here's an example I did for https://github.com/chrisdew/protobuf/issues/29 - most users won't need the complication of bytes fields.
buftest.proto
package com.chrisdew.buftest;
message BufTest {
optional float num = 1;
optional bytes payload = 2;
}
buftest.js
var fs = require('fs');
var Schema = require('protobuf').Schema;
// "schema" contains all message types defined in buftest.proto|desc.
var schema = new Schema(fs.readFileSync('buftest.desc'));
// The "BufTest" message.
var BufTest = schema['com.chrisdew.buftest.BufTest'];
var ob = { num: 42 };
ob.payload = new Buffer("Hello World");
var proto = BufTest.serialize(ob);
console.log('proto.length:', proto.length);
var outOb = BufTest.parse(proto);
console.log('unserialised:', JSON.stringify(outOb));
var payload = new Buffer(outOb.payload);
console.log(payload);
Makefile: (second line begins with a TAB not spaces)
all:
protoc --descriptor_set_out=buftest.desc --include_imports buftest.proto
output:
$ node buftest.js
proto.length: 18
unserialised: {"num":42,"payload":{"0":72,"1":101,"2":108,"3":108,"4":111,"5":32,"6":87,"7":111,"8":114,"9":108,"10":100,"length":11}}
payload: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>