protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

util.Long is not a constructor

Open gun27311 opened this issue 2 years ago • 11 comments

https://github.com/protobufjs/protobuf.js/blob/2cdbba32da9951c1ff14e55e65e4a9a9f24c70fd/src/util/longbits.js#L116

? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned)) //new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))// new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
          ^

TypeError: util.Long is not a constructor

i try to fix that import lib long

const long = require("long") 

new long(this.lo | 0, this.hi | 0, Boolean(unsigned))

it work

gun27311 avatar Jun 15 '22 09:06 gun27311

Running into this as well...not sure what the fix is beyond the post above.

molivertsla avatar Jul 12 '22 19:07 molivertsla

Ended up leveraging patch-package but it seems like something is overwriting util.Long in src/util/minimal.js where the Long constructor is actually the default import, however the entire module is being imported.

--- a/node_modules/protobufjs/src/util/longbits.js
+++ b/node_modules/protobufjs/src/util/longbits.js
@@ -112,9 +112,11 @@ LongBits.prototype.toNumber = function toNumber(unsigned) {
  * @returns {Long} Long
  */
 LongBits.prototype.toLong = function toLong(unsigned) {
-    return util.Long
+    return typeof util.Long === 'function'
         ? new util.Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
         /* istanbul ignore next */
+        : util.Long.default && typeof util.Long.default === 'function'
+        ? new util.Long.default(this.lo | 0, this.hi | 0, Boolean(unsigned))
         : { low: this.lo | 0, high: this.hi | 0, unsigned: Boolean(unsigned) };
 };

molivertsla avatar Jul 13 '22 17:07 molivertsla

Ran into the above issue, tried going back to an older version of protobufjs to no luck, but thankfully found a nice workaround:

import Long from "long";
import protobufjs from "protobufjs";

protobufjs.util.Long = Long;
protobufjs.configure();

after adding the above before i do any more grpc stuff, it seems to work correctly

jackharrhy avatar Jul 31 '22 00:07 jackharrhy

I have the same issue. I am on version 6.11.3. Curiously I'm using this in two different places. One is my large application where I'm using webpack and electron. That one I see the issue. The other is in a small test application that doesn't have many other dependencies. In that small one, I do not see this issue.

I am using @jackharrhy workaround above with success.

danslinger avatar Aug 09 '22 18:08 danslinger

Still occurs with 7.2.3

kelteseth avatar Apr 07 '23 15:04 kelteseth

Yes, getting the same error on my gRPC client.

Error: 13 INTERNAL: Response message parsing error: util.Long is not a constructor
    at callErrorFromStatus (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/call.ts:81:17)
    at Object.onReceiveStatus (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/client.ts:356:55)
    at Object.onReceiveStatus (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/client-interceptors.ts:455:34)
    at Object.onReceiveStatus (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/client-interceptors.ts:415:48)
    at /Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/resolving-call.ts:111:24
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
for call at
    at ServiceClientImpl.makeUnaryRequest (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/client.ts:326:30)
    at ServiceClientImpl.getProductionByWell (/Users/me/wd/grpc-poc/client-ts/node_modules/@grpc/grpc-js/src/make-client.ts:189:15)
    at /Users/me/wd/grpc-poc/client-ts/test/main.ts:16:27
    at new Promise (<anonymous>)
    at main (/Users/me/wd/grpc-poc/client-ts/test/main.ts:15:10) {
  code: 13,
  details: 'Response message parsing error: util.Long is not a constructor',
  metadata: Metadata { internalRepr: Map(0) {}, options: {} }
}

Node: v16.16.0 protobufjs: v7.2.3

The workaround suggested by @jackharrhy did not work for me. Are there any other known workarounds, or plans to fix this?

maikelrobier avatar Apr 20 '23 23:04 maikelrobier

UPDATE: Missing context from my previous comment: I am using ts-node and generating with ts-proto.

Solution (or workaround): I set the opt esModuleInterop=true and it fixed it for me.

maikelrobier avatar Apr 20 '23 23:04 maikelrobier

Not sure what to do, because the workaround with configure

	import Long from 'long';
	import protobufjs, { load } from 'protobufjs';
	protobufjs.util.Long = Long;
	protobufjs.configure();

does not work when running npm run build

TypeError: Cannot set properties of undefined (setting 'Long')
    at file:///C:/Code/test/frontend/.svelte-kit/output/server/chunks/messages.js:3:15
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)

node:internal/event_target:1037
  process.nextTick(() => { throw err; });

kelteseth avatar Jun 01 '23 12:06 kelteseth

@molivertsla pinpointed the underlying issue in his comment: https://github.com/protobufjs/protobuf.js/issues/1745#issuecomment-1183493007

The issue appears to be incompatible versions of Long and protobufjs.

In my case, it was because ts-proto was using an older version of Long. When I updated ts-proto to a newer version, the problem went away.

jcbrand avatar Oct 18 '23 11:10 jcbrand