bitcoinjs / bip39

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
ISC License
1.11k stars 447 forks source link

Cannot read property 'wordlists' of undefined #122

Closed DalderupMaurice closed 5 years ago

DalderupMaurice commented 5 years ago

Upgrading from 2.6.0 to 3.0.2 - nothing is accessible anymore through the following method (the one mentioned in the README):

import bip39 from "bip39"; // equivalent of const bip39 = require("bip39");

console.log(bip39.wordlists); // --> bip39 is undefined as it doesn't have a default export

It is however accessible by using:

import { wordlists } from "bip39";

console.log(wordlists);

or

import * as bip39 from "bip39";

console.log(bip39.wordlists);

Just an FYI as this change is quite breaking for some applications (including the ones I was working on) but it was nowhere documented

junderw commented 5 years ago

import bip39 from "bip39";

is equivalent to

const bip39 = require('bip39').default

you want

import * as bip39 from "bip39";

DalderupMaurice commented 5 years ago

I know, but it worked using that way in the previous release.. Also, in the readme - it's stated to use const bip39 = require("bip39"); - which does not work.

this means tons of projects are probably using it this way, they update the dependency and discover it is broken because the import doesn't work as it used to.

This breaking change is nowhere documented as it should.

This issue should either be addressed by adjusting the readme and mentioning this fundamental change in the changelog. Or by re-introducing the functionality so that the import works as it did in the previous releases.

junderw commented 5 years ago

Started up a new docker container with node v10 and installed bip39 v3.0.2

This works

> const bip39 = require('bip39')
> bip39.generateMnemonic()
'library burger magnet trigger trigger fish repair viable tennis misery detect fame'
> Object.keys(bip39.wordlists)
[ 'chinese_simplified',
  'chinese_traditional',
  'korean',
  'french',
  'italian',
  'spanish',
  'japanese',
  'JA',
  'english',
  'EN' ]

Then I installed ts-node

> import * as bip39 from 'bip39';
> bip39.generateMnemonic()
'ivory neither image cargo usage taxi wagon vessel truth cook neutral side'
> Object.keys(bip39.wordlists)
[ 'chinese_simplified',
  'chinese_traditional',
  'korean',
  'french',
  'italian',
  'spanish',
  'japanese',
  'JA',
  'english',
  'EN' ]

The current index.js (which is auto-generated by TypeScript)

https://github.com/bitcoinjs/bip39/blob/cfea218ee2e6c3157baabb1e2ec684d36cce89c5/src/index.js#L40

https://github.com/bitcoinjs/bip39/blob/cfea218ee2e6c3157baabb1e2ec684d36cce89c5/src/index.js#L58

https://github.com/bitcoinjs/bip39/blob/cfea218ee2e6c3157baabb1e2ec684d36cce89c5/src/index.js#L94

https://github.com/bitcoinjs/bip39/blob/cfea218ee2e6c3157baabb1e2ec684d36cce89c5/src/index.js#L121

https://github.com/bitcoinjs/bip39/blob/cfea218ee2e6c3157baabb1e2ec684d36cce89c5/src/index.js#L159

These are the exports of an older version

https://github.com/bitcoinjs/bip39/blob/b524d346f97a6e364b251d6115a68e3362c88f4b/index.js#L156-L178

since exports is an alias for module.exports you can see they are equivalent.

junderw commented 5 years ago

Also, the README states const bip39 = require('bip39')

The README does not state import bip39 from "bip39";

junderw commented 5 years ago

I can not reproduce. So if you can create a test repo where I can just run npm install and npm test and see what's going on, I'll be glad to look into it further.

I am not seeing any breakage.

junderw commented 5 years ago

Also, on the topics of default exports, which we have never supported.

https://basarat.gitbooks.io/typescript/content/docs/tips/defaultIsBad.html

lukechilds commented 5 years ago

@DalderupMaurice are you using TypeScript or a custom build system? If a custom build system can you share your config?