thibauts / node-castv2

An implementation of the Chromecast CASTV2 protocol
MIT License
774 stars 100 forks source link

TypeError: Cannot read property 'build' of null #46

Closed nbetcher closed 6 years ago

nbetcher commented 7 years ago

Hello, When using 'pkg' to package an application together into a static executable, the castv2 library is throwing this error upon execution of said static executable:

TypeError: Cannot read property 'build' of null
    at Object.<anonymous> (/snapshot/nbetcher/node_modules/castv2/lib/proto.js:5:26)
    at Module._compile (pkg/prelude/bootstrap.js:1243:22)
    at Object.Module._extensions..js (module.js:644:10)
    at Module.load (module.js:552:32)
    at tryModuleLoad (module.js:495:12)
    at Function.Module._load (module.js:487:3)
    at Module.require (module.js:577:17)
    at Module.require (pkg/prelude/bootstrap.js:1153:31)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/snapshot/nbetcher/node_modules/castv2/lib/client.js:5:27)

Which boils down to these two lines:

var builder = ProtoBuf.loadProtoFile(__dirname + "/cast_channel.proto");
var extensions = builder.build('extensions.api.cast_channel');

Now I'm a total NodeJS noob and I really have no idea how 'pkg' works (e.g. if it virtualizes a file system, or how exactly), but operating on the theory that it can't "read" that file since it's all one static package, I simplified the contents of cast_channel.proto (e.g. removed new lines, comments, etc), put the contents into a variable, and used loadProto (which accepts a string instead of file contents). This method worked once packaged into a static executable.

If you consider this a defect with pkg, please let me know, but a workaround for others is probably nice too. :-)

Flo-Razor commented 6 years ago

Hello @nbetcher,

I encountered the same issue. I tried to solve this issue but anything works. Can you show me your fix ?

Thank you ;)

LeeStevens318 commented 6 years ago

Ok, figured it out. First i ran the pkg with debug with a grep '/castv2/lib' and could see only certain files in the lib folder being added. pkg index.js --out-put out --debug | grep castv2/lib

To fix you need to use path.join instead of __dirname + '/cast_channel.proto'. var builder = ProtoBuf.loadProtoFile(path.join(__dirname, "/cast_channel.proto"));

You also need to include path var path = require('path');

Not sure how to do a pull request, if someone could to fix this instead of manually editing would be awesome.

nbetcher commented 6 years ago

I haven't done a pull request in a long time, but if you're determined enough to fix the problem then a pull request is easy. ;-)