dignifiedquire / borc

Assimilate your JavaScript objects into cbor
https://dignifiedquire.github.io/borc
MIT License
31 stars 22 forks source link

TypeError: Bignumber is not a constructor #41

Closed metacoin closed 5 years ago

metacoin commented 5 years ago

When using borc in an electron webpack app, I am getting the error TypeError: Bignumber is not a constructor.

When inspecting Bignumber with console.log in constants.js, I noticed this:

┏ Electron -------------------
  Bignumber:  Object [Module] {
    BigNumber: [Getter],
    default:
     { [Function: BigNumber]
       clone: [Function: clone],
       ROUND_UP: 0,
       ROUND_DOWN: 1,
       ROUND_CEIL: 2,
       ROUND_FLOOR: 3,
       ROUND_HALF_UP: 4,
       ROUND_HALF_DOWN: 5,
       ROUND_HALF_EVEN: 6,
       ROUND_HALF_CEIL: 7,
       ROUND_HALF_FLOOR: 8,
       EUCLID: 9,
       set: [Function],
       config: [Function],
       isBigNumber: [Function],
       max: [Function],
       maximum: [Function],
       min: [Function],
       minimum: [Function],
       random: [Function],
       sum: [Function] } }
┗ ----------------------------

I changed this line: https://github.com/dignifiedquire/borc/blob/b14a3176cc2851185320c33ccaa0020746beacab/src/constants.js#L3

From this:

const Bignumber = require('bignumber.js')

to this:

const Bignumber = require('bignumber.js').default

I did the same thing here: https://github.com/dignifiedquire/borc/blob/b14a3176cc2851185320c33ccaa0020746beacab/src/encoder.js#L7

I am pretty new to electron and webpack and not sure why I had to do this, but perhaps changing the code for compatibility would be good for others creating electron apps or using tools similar to webpack. Or, some guidance on how I can avoid these errors would be appreciated. Thanks!

hugomrdias commented 5 years ago

can you please confirm that all of versions bellow work in an electron enviroment ?

const Bignumber = require('bignumber.js').default
const {Bignumber} = require('bignumber.js')
const Bignumber = require('bignumber.js').BigNumber
metacoin commented 5 years ago

const {Bignumber} = require('bignumber.js') Results in the same error as above:

┏ Electron -------------------

  A JavaScript error occurred in the main process
  Uncaught Exception:
  TypeError: Bignumber is not a constructor
      at eval (webpack-internal:///../../node_modules/borc/src/constants.js:62:19)

const Bignumber = require('bignumber.js').BigNumber does seem to work just as well as .default, no error reported

hugomrdias commented 5 years ago

do you know what MainFields order is used by webpack for electron ? https://webpack.js.org/configuration/resolve/#resolvemainfields

metacoin commented 5 years ago

I'm not sure what the default is. Right now, my webpack.config.js file is completely empty, if that helps. I will be looking more closely at this later on... will report back with any findings

hugomrdias commented 5 years ago

@olizilla can you help with this? Webpack with electron doesn't follow browser > module> main by default?

elantion commented 5 years ago

I am facing the same issue in an almost brand new project. Using electron, vue-cli and ipfs-js. It's very easy to reproduce this issue.

  1. Install vue-cli: yarn global add @vue/cli.
  2. Create a project: vue create test.
  3. Add electron support: vue add electron-builder.
  4. Put test code in 'ready' event callback at background.js.
app.on('ready', async () => {
  if (isDevelopment && !process.env.IS_TEST) {
    // Install Vue Devtools
    try {
      await installVueDevtools()
    } catch (e) {
      console.error('Vue Devtools failed to install:', e.toString())
    }
  }
  createWindow();
  const ipfs = new IPFS;
  ipfs.on('ready', ()=>{
      console.log('ready');
      resolve(ipfs);
  });
})
  1. Run with yarn electron:serve.
  2. You can see a error like below:
App threw an error during load
TypeError: Bignumber is not a constructor
    at eval (webpack:///./node_modules/borc/src/constants.js?:62:19)
    at Object../node_modules/borc/src/constants.js (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:1760:1)
    at __webpack_require__ (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:20:30)
    at eval (webpack:///./node_modules/borc/src/utils.js?:5:19)
    at Object../node_modules/borc/src/utils.js (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:1855:1)
    at __webpack_require__ (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:20:30)
    at eval (webpack:///./node_modules/borc/src/decoder.js?:7:15)
    at Object../node_modules/borc/src/decoder.js (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:1783:1)
    at __webpack_require__ (/Users/yinjames/Projects/lc-ipfs/dist_electron/index.js:20:30)
    at eval (webpack:///./node_modules/borc/src/diagnose.js?:3:17)
haywirez commented 5 years ago

Also encountering this issue, part of an IPFS-related package...seems to affect webpack in general

ishan-ghorela commented 3 years ago

hi I am still facing this issue