illegal token '<' (/demo.proto, line 1)
protobuf.js version: <6.8.6>
protobuf.load throw an Error illegal token '<' (/demo.proto, line 1)
package demopackage;
syntax = "proto3";
message DemoMessage {
string name = 1;
fixed32 age = 2;
bool flag = 3;
enum TYPE {
A = 0;
B = 1;
}
TYPE type = 4;
float score = 5;
SubDemoMessage sub = 6;
}
message SubDemoMessage {
string subName = 1;
fixed32 subAge = 2;
}
protobuf.load(path.join(__dirname, 'demo.proto'), (err, root) => {
if (err) {
throw err
}
const DemoMessage = root.lookupType('demopackage.DemoMessage')})
stack trace of the error
Uncaught Error: illegal token '<' (/demo.proto, line 1)
at illegal (parse.js?a291:94)
at parse (parse.js?a291:733)
at process (root.js?c380:107)
at eval (root.js?c380:182)
at XMLHttpRequest.fetchOnReadyStateChange (index.js?4b75:103)
when I execute the file alone, it works fine. but when I run it with roadhog server in a react web project, it just throw the error.
Can you help me? thks!
That seems like whatever bundler this is using behind the scenes is adding a < as the first character, like if it was an html file. Probably not related to protobuf.js directly.
thks, I transfer .proto to .json, and use protobuf.Root.fromJSON, it works! but I have another question. When nodeJS communicates with the browser, how do I Decode an Uint8Array (browser) in nodeJS and decode Buffer (node) in browser?
or what is the best practice for Node and browser communication with protobufJS?
thks!
How to deal with this with proto files?
I have this issue too, by looking into the source code, I found that:
- The
protobuf.loadmethod callsutil.fetchand get data from network - And the
protobuf.loadSyncmethod calls 'util.fs.readFileSync' and get data from file system, and in the comments saysSynchronously loads one or multiple .proto or preprocessed .json files into this root namespace ( **node only** ).
So if we use this protobuf.js in browser, we fetch the .proto file from network , like util.fetch("http://your_current_domain.com" + "filename")
If you provide a filename like "../../file.proto", and you will get the index.html, its content will be pass to a parse(filename, source) function, because the value of source parameter is the content of the index.html file, and this issue occur ... (The first character of source is '<')
Hello, have you solved this problem? @NeoyeElf @gogolxdong @zhangtiny123 我是在vue.js里面使用的 protobuf.load Uncaught Error: illegal token '<' (awesome.proto, line 1)
transfer .proto to .json, and use protobuf.Root.fromJSON. @BigEgret
@BigEgret Try to make your awesome.proto file be accessible from url
http://your.domain.com/path/to/awesome.proto
and
protobuf.load('/path/to/awesome.proto', (err, root) => {})
@zhangtiny123 @NeoyeElf Thank you!
I found another work-around for use with node (or React in my case). import your proto-file and just pass it as an argument to .load():
import proto from "./awsome.proto" protobuf.load(proto, (err, root) => {})
Is this just a codesandbox problem? or has anyone got a working sandbox which they can share?
@BigEgret Try to make your
awesome.protofile be accessible from url
http://your.domain.com/path/to/awesome.protoand
protobuf.load('/path/to/awesome.proto', (err, root) => {})
You can drop your .proto file in the public directory (the root directory of your website) just near the index.html and load it just with load('demo.proto',function(err,root){/** **/});
Or you can create sub directories to organize your code like @zhangtiny123 shown in his example.
This worked for me:
const protoFile= require("./some_file.proto");
protobuf.load(protoFile, (err, root) => {})
@srkigres Could you share your webpack setup?
The core of the problem lies in the normalize method in the @protobufjs/path dependency, which will replace "://" in the path with ":/", causes an exception when requesting the resource address, pointing to the service where index.html is located .
thks, I transfer .proto to .json, and use
protobuf.Root.fromJSON, it works! but I have another question. When nodeJS communicates with the browser, how do I Decode an Uint8Array (browser) in nodeJS and decode Buffer (node) in browser?or what is the best practice for Node and browser communication with protobufJS?
thks!
can you send me the reference code how you fixed it.