I am currently having a TypeScript project that includes Ethers.js, which in turn includes bn.js.

The problem is

SyntaxError: The requested module './../../../bn.js/lib/bn.js' does not provide an export named 'default'

It appears to me this is because BN is in UMD format (see at https://github.com/indutny/bn.js/blob/master/lib/bn.js#L1)

(function (module, exports) {
 'use strict';
 // Utils
 function assert (val, msg) {
   if (!val) throw new Error(msg || 'Assertion failed');
 }

and the correponding .ts declaration is

"use strict";

/**
 *  BigNumber
 *
 *  A wrapper around the BN.js object. We use the BN.js library
 *  because it is used by elliptic, so it is required regardless.
 *
 */

import _BN from "bn.js";
import BN = _BN.BN;

import { Bytes, Hexable, hexlify, isBytes, isHexString } from "@ethersproject/bytes";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

It may be there's something that could be done at importing (source) or in Rollup. Difficult to tell!

Here is a screenshot of the build errors (one variation, depending on if building or running directly)

screenshot of the build errors

Question: Is there a way to transform this format to an ESM format in application Rollup pipeline?

I have tried using @rollup/plugin-commonjs and @rollup/plugin-node-resolve as in

resolve({ browser: true, preferBuiltins: false }), commonjs()]

(or see the project as whole at https://github.com/veikkoeeva/erc1155sample/blob/main/web/rollup.config.js, the error shows with npm run test or npm run start (in console log)).

Thus far I've had no luck cracking this, though. Hence coming here wondering if there's a dumb issue I don't see or if this is a genuinely tougher issue.

Edit: indeed, following https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module and maybe named exports is the key here...

0

There are 0 best solutions below