polkadot-js / common

Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers.
Apache License 2.0
254 stars 145 forks source link

Can't import the named export 'BN_BILLION' from non EcmaScript module (only default export is available) #900

Closed outlier89 closed 3 years ago

outlier89 commented 3 years ago

When trying to import @polkadot/api in a nuxt project, cannot complete a successful build. All 141 errors are "Can't import..." as shown below:

 ERROR  Failed to compile with 141 errors                                                                                                          friendly-errors 10:34:57  

 ERROR  in ./node_modules/@polkadot/types/codec/AbstractInt.mjs                                                                                    friendly-errors 10:34:57  

Can't import the named export 'BN_BILLION' from non EcmaScript module (only default export is available)                                           friendly-errors 10:34:57  
                                                                                                                                                   friendly-errors 10:34:57  

 ERROR  in ./node_modules/@polkadot/util-crypto/hd/ledger/derivePrivate.mjs                                                                        friendly-errors 10:34:57  

Can't import the named export 'BN_EIGHT' from non EcmaScript module (only default export is available)......

Expected behavior

Should build

Current behavior

Does not build

Code to reproduce for BUG REPORT

import { ApiPromise, WsProvider } from '@polkadot/api';

Environment

Executable Version
nuxt --version 2.14.12
npm --version 6.14.6
node --version 12.8.3
OS Win 10
caosbad commented 3 years ago

Got the same error .... @outlier89 do you fix that problem?

jacogr commented 3 years ago

Using WP4, you need to add the following to get it to recognise .mjs -

{
    test: /\.mjs$/,
    include: /node_modules/,
    type: "javascript/auto"
}

Previous issues with more information are -

caosbad commented 3 years ago

@jacogr Thx , thats work

jacogr commented 3 years ago

Not the next release, but the release after that (i.e. scheduled for Monday after-next), the packages will move to "type": "module" globally which means the .js extension is used for modules and this override would become unneeded on slightly older bundlers. (This is assuming testing going 100% to plan on that, thus far it looks good, but also don't want to rush it.)

Sadly the support for all platforms with both CommonJS and ESM modules is still not as smooth as it should be, while WP5 has full support (as latest rollup, parcel, ...), not all tooling is on latest, e.g. create-react-app. The ESM has huge benefits though, i.e. the bundlers can create much smaller outputs. It has just been slightly rocky implementing...

outlier89 commented 3 years ago

@jacogr Works perfectly. Great going guys. Keep it up.

polkadot-js-bot commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.