Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert proto files to js format using pbjs #522

Closed
Arun-KumarH opened this issue Dec 7, 2016 · 6 comments
Closed

Cannot convert proto files to js format using pbjs #522

Arun-KumarH opened this issue Dec 7, 2016 · 6 comments

Comments

@Arun-KumarH
Copy link

I am trying to convert .proto file to javascript target but I am getting below error :
My protobufjs version is:
protobuf.js v6.0.2 cli

$ pbjs cat.proto -target=js > cat_pb.js
module.js:474
    throw err;
    ^
Error: Cannot find module '/restore/test/js'
    at Function.Module._resolveFilename (module.js:472:15)
    at Function.Module._load (module.js:420:25)
    at Module.require (module.js:500:17)
    at require (internal/module.js:20:19)
    at Object.exports.main (/usr/lib64/node_modules/protobufjs/cli/pbjs.js:64:18)
    at Object.<anonymous> (/usr/lib64/node_modules/protobufjs/bin/pbjs:4:16)
    at Module._compile (module.js:573:32)
    at Object.Module._extensions..js (module.js:582:10)
    at Module.load (module.js:490:32)
    at tryModuleLoad (module.js:449:12)

My proto file is below :

syntax = "proto3";
package demo;
message Cat {
   required string name = 1;
   required     int32 age = 2;
	message Parent {
		required     string name = 1;
	        required     string email = 2;
			}
	repeated Parent parents =4;
}

Please suggest.

@dcodeIO
Copy link
Member

dcodeIO commented Dec 7, 2016

protobuf.js 6's pbjs does not have a js target. It has proto2, proto3, json and static. A classes target similar to js of protobuf.js 5 is planned but would benefit a lot from generation multiple files (one for each class, but currently pbjs generates a single file only).

@Arun-KumarH
Copy link
Author

Arun-KumarH commented Dec 7, 2016

So which versions of pbjs currently support the target js / commonjs ?
When is the expected planned release for pbjs support for prtobuf.js 6's version ?

Any suggestion to generate javascript files from proto files with version 6 (even it is single file should not be a problem).

@dcodeIO
Copy link
Member

dcodeIO commented Dec 7, 2016

The js target you are looking for is currently available with protobuf.js 5. Technically, it is not much different from what is described here for protobuf.js 6, which uses the json target and loads its output through Root.fromJSON.

The js target of protobuf.js 5 basically just loads the json output in a CommonJS wrapper.

I'd recommend that you use protobuf.js 6 with the json target and just wrap this up as:

module.exports = require("protobufjs").Root.fromJSON(%INSERT_GENERATED_JSON_HERE%);

(I might even add a quick target for that)

@dcodeIO
Copy link
Member

dcodeIO commented Dec 7, 2016

You can use json-module now, which is basically what js was before. Use the --wrap argument in case you'd like to have a custom CommonJS-only wrapper.

@Arun-KumarH
Copy link
Author

Does js here mean json-module for java script. I was looking for java script file which would be generated from pbjs some thing like protoc --proto_path=. --js_out=import_style=commonjs,binary:. test.proto - so a file similar to test_pb.js file which would be generated by protoc but using pbjs on cli, is that not possible using pbjs ?

@dcodeIO
Copy link
Member

dcodeIO commented Dec 7, 2016

For static code, use the static-module target. This is still somewhat experimental, though, so let me know of any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants