protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
330 stars 66 forks source link

How to use Closure imports with normal node.js apps? #164

Closed SetoKaiba closed 1 year ago

SetoKaiba commented 1 year ago
https://developers.google.com/closure/library

The doc only mentions how to use closure with browser. How to use it with normal node.js apps?

I npm install google-closure-library and google-protobuf. I generated the myproto_libs.js with protoc-gen-js.

How to use the myproto_libs.js with it? I know that I can use commonjs style. And it works. But I'd like to know the closure style usage.

require('google-closure-library');
require('google-protobuf');

goog.require("goog.crypt.Sha1");

var sha1 = new goog.crypt.Sha1();
sha1.update("foobar");
var hash = sha1.digest();
console.log(hash);

goog.require('proto.Test');
let test = proto.Test();
console.log(test);
[
  136,  67, 215, 249,  36,  22,
   33,  29, 233, 235, 185,  99,
  255,  76, 226, 129,  37, 147,
   40, 120
]
goog.require could not find: proto.Test
E:\WebstormProjects\test-node-js\index.js:12
let test = proto.Test();
           ^

ReferenceError: proto is not defined
    at Object.<anonymous> (E:\WebstormProjects\test-node-js\index.js:12:12)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
dibenede commented 1 year ago

Can you tell us about your use case? Generally speaking, these are different module systems and I'm not sure if there's a way to get node to understand closure modules.

SetoKaiba commented 1 year ago

I want to use it with Angular. I'm not sure what's the preferred way to use with it. I thought that the closure is a google product as well. It maybe better with Angular, protobuf-javascript and Closure.

dibenede commented 1 year ago

From asking around and doing a bit of digging online, it seems like the best option is to use google-protobuf with commonjs imports with angular. Our understanding is that most users rely on commonjs for bundling.