简体   繁体   中英

TypeError: bn_js_1.default is not a constructor

Hello everone, i just implement bn.js in class validator DTO. And got some error like this:

TypeError: bn_js_1.default is not a constructor at Object.transformFn

anyone can help me? here's the DTO:

import BN from 'bn.js';
import { Transform } from 'class-transformer';

export class IWCreateNftCollectionV1ReqDTO {
  @Transform(({ value }) => new BN(value.toString()))
  royalties:BN;
}

And the package.json

"dependencies": {
    "@types/bn.js": "^5.1.0",
    "bn.js": "^5.2.0"
}

Thanks

UPDATE Solved by adding bellow in tsconfig.json

{
  "compilerOptions": {
    ...
    "esModuleInterop": true,
  },
}

Seem like bn.js doesn't use default exports. You can either set esModuleInterop: true in your tsconfig , or you should be able to do import * as BN from 'bn.js'; if you don't want to modify that setting

Add the setting esModuleInterop: true in the tsconfig file worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM