json-bigint icon indicating copy to clipboard operation
json-bigint copied to clipboard

Unable to use with TypeScript

Open bushev opened this issue 5 years ago • 14 comments

Hi,

The module is great! But I can't use it with a TypeScript Lambda function on AWS. It fails in runtime with error:

TypeError: Right-hand side of 'instanceof' is not callable
    at str (***:/node_modules/json-bigint/lib/stringify.js:218:1)
    at Function.module.exports.JSON.stringify (***:/node_modules/json-bigint/lib/stringify.js:380:1)
    at Response.toLambdaResponse (***:/_core/response.ts:91:71)
    at LambdaHandler.execute (***:/_core/LambdaHandler.ts:123:47)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)

My tsconfig.json is

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "target": "esnext",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "declaration": false,
    "listFiles": false,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "lib": [
      "esnext",
      "es2019"
    ]
  }
}

bushev avatar May 19 '20 02:05 bushev

Thanks for report @bushev ! would you be able to make a simple repo ready to reproduce issue?

sidorares avatar May 19 '20 02:05 sidorares

Hey Andrey,

please, check it out: https://github.com/bushev/json-bigint/tree/master/issue

bushev avatar May 19 '20 03:05 bushev

hm, the problem is in a way how webpack bundles bignumber.js

If I replace first line in stringify.js to var BigNumber = require('bignumber.js').default; then your example works ( but obviously native node CJS examples don't )

Not sure yet how to solve this for both node and webpack consumers

sidorares avatar May 19 '20 04:05 sidorares

I did a simple fix based on the details you provided. It works well for me - https://github.com/bushev/json-bigint/commit/e3e93f71d633cd558bc7c57afcd9e1f54f669d0b

bushev avatar May 19 '20 22:05 bushev

Any updates on this? I'm facing with the same issue.

cr0cK avatar Apr 20 '21 14:04 cr0cK

Another solution that works with the usage of patch-package:

$ cat patches/json-bigint+1.0.0.patch 
diff --git a/node_modules/json-bigint/lib/stringify.js b/node_modules/json-bigint/lib/stringify.js
index 3bd5269..b4a3319 100644
--- a/node_modules/json-bigint/lib/stringify.js
+++ b/node_modules/json-bigint/lib/stringify.js
@@ -215,7 +215,7 @@ var JSON = module.exports;
             mind = gap,
             partial,
             value = holder[key],
-            isBigNumber = value != null && (value instanceof BigNumber || BigNumber.isBigNumber(value));
+            isBigNumber = value != null && (value instanceof BigNumber.default || BigNumber.default.isBigNumber(value));
 
 // If the value has a toJSON method, call it to obtain a replacement value.

cr0cK avatar Apr 20 '21 15:04 cr0cK

i have this issue too , error: BigNumber is not a constructor

zhfnjust avatar May 05 '21 04:05 zhfnjust

Same issue.

ivands avatar Jun 08 '21 10:06 ivands

For now you can use "@types/json-bigint": "^1.0.1" in typescript, than import { parse as jsonBigIntParse, stringify as sonBigIntStringify, } from 'json-bigint'; and its working very well.

NetMajom avatar Nov 13 '21 11:11 NetMajom

For now you can use "@types/json-bigint": "^1.0.1" in typescript, than import { parse as jsonBigIntParse, stringify as sonBigIntStringify, } from 'json-bigint'; and its working very well.

Unfortunately I still get the error when using this solution in AWS Lambda

2022-05-11T07:31:09.601Z	942f9df2-3e1c-4939-9e98-d56e7e6f4b3d	INFO	TypeError: Right-hand side of 'instanceof' is not callable
    at p (/var/task/app.js:2:823196)
    at a.stringify.a.stringify (/var/task/app.js:2:824298)
    at /var/task/app.js:8:10524
    at Generator.next (<anonymous>)
    at /var/task/app.js:8:9856
    at new Promise (<anonymous>)
    at i (/var/task/app.js:8:9601)
    at t.matchPools (/var/task/app.js:8:10007)
    at /var/task/app.js:8:9222

tonisives avatar May 11 '22 07:05 tonisives

I did a simple fix based on the details you provided. It works well for me - bushev@e3e93f7

The fix fixed the issue. When will you update the npm version?

ifgeny87 avatar Aug 11 '22 07:08 ifgeny87

Good brother, when will you update the npm version?

CHENGCHANGHU avatar Mar 24 '23 08:03 CHENGCHANGHU

Confirmed that https://github.com/bushev/json-bigint/commit/e3e93f71d633cd558bc7c57afcd9e1f54f669d0b fixes this issue for me.

I published a fork with the fixes: https://www.npmjs.com/package/@toniq-labs/json-bigint

electrovir avatar Apr 08 '23 01:04 electrovir