Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vikiival opened this issue Jan 13, 2021 · 11 comments
Labels

Comments

@vikiival
Copy link

Hey I have problem to use @polkadot/extension-dapp

Using tutorials:
https://polkadot.js.org/docs/extension/usage
https://dotleap.com/building-and-address-converter-page-polkadot/

Screen Shot 2021-01-13 at 10 46 50

@jacogr

From console

 error  in ./node_modules/@polkadot/util-crypto/mnemonic/bip39.mjs

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

 error  in ./node_modules/@polkadot/util-crypto/base64/encode.mjs

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

 error  in ./node_modules/@polkadot/util-crypto/nacl/sign.mjs

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

 error  in ./node_modules/@polkadot/util-crypto/crypto.mjs

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

 error  in ./node_modules/@polkadot/ui-keyring/index.mjs

Can't reexport the named export 'Ledger' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/wasm-crypto-wasm/buffer.mjs

Can't reexport the named export 'buffer' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/keyring/index.mjs

Can't reexport the named export 'decodeAddress' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/keyring/index.mjs

Can't reexport the named export 'encodeAddress' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/keyring/index.mjs

Can't reexport the named export 'setSS58Format' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/wasm-crypto-wasm/buffer.mjs

Can't reexport the named export 'sizeCompressed' from non EcmaScript module (only default export is available)

 error  in ./node_modules/@polkadot/wasm-crypto-wasm/buffer.mjs

Can't reexport the named export 'sizeUncompressed' from non EcmaScript module (only default export is available)
@jacogr
Copy link
Member

jacogr commented Jan 13, 2021

This means your bundler resolves .mjs files, however it doesn't know that they are ESM modules.

On webpack, you can add the following to rules -

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

PS: CRA, not that you use it, but for future reference, has a particularly brain-dead setup where is specifically resolves mjs and doesn't have a handler for it, see https://stackoverflow.com/questions/64002604/how-to-make-create-react-app-support-mjs-files-with-webpack (Step-through CRA here polkadot-js/common#812 (comment))

@jacogr jacogr added the support label Jan 13, 2021
@vikiival
Copy link
Author

@jacogr do you have please any hint how to use it vue.config ?

@jacogr
Copy link
Member

jacogr commented Jan 13, 2021

According to some light googling and clicking the first link, YMMV - nklayman/vue-cli-plugin-electron-builder#67 (comment)

@yangwao
Copy link

yangwao commented Jan 13, 2021

Find the same as well, but yield same results, what you've googled? I can dig around more, maybe I'm missing something.

Following https://cli.vuejs.org/guide/webpack.html#simple-configuration

module.exports = {
  publicPath: './',
  configureWebpack: {
    include: '/node_modules/',
    test: '/\.mjs$/',
    type: 'javascript/auto'
  }
}

Following nklayman/vue-cli-plugin-electron-builder#67 (comment)

module.exports = {
  publicPath: './',
  chainWebpackMainProcess: (config) => {
    config.module.rule('javascript/auto').test(/\.mjs$/)
  }
}

@jacogr
Copy link
Member

jacogr commented Jan 13, 2021

I believe, looking at the Vue docs (not an expert, but following an example here https://cli.vuejs.org/guide/html-and-static-assets.html#prefetch), you can probably do -

// vue.config.js
module.exports = {
  chainWebpack: (config) => {
    config.module.rule('javascript/auto').test(/\.mjs$/);
  }
}

@vikiival
Copy link
Author

also added this and it still does not work

  chainWebpack: (config) => {
    config.module.rule('javascript/auto').test(/\.mjs$/).include
    .add(/node_modules/)
    .end();
  }

@yangwao
Copy link

yangwao commented Jan 13, 2021

Either upper snippet either this didn't worked. Trying knocking out issue, if we don't have old dependencies, but we have @vue/cli-service-'4.2.3': '2020-02-27T14:34:06.315Z', which should include this PR vuejs/vue-cli#2819 when it was added. But may we are missing some cli-plugin from https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue, time for little shopping around.

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('javascript/auto')
      .test(/\.mjs$/);
  }

@jacogr
Copy link
Member

jacogr commented Jan 13, 2021

Here is a another example for the glahql library - graphql/graphql-js#1272 (comment)

module.exports = {
    chainWebpack: config => {
        // ...other chains
        config.module // fixes https://github.com/graphql/graphql-js/issues/1272
            .rule('mjs$')
            .test(/\.mjs$/)
            .include
                .add(/node_modules/)
                .end()
            .type('javascript/auto');
    },
    configureWebpack: {
        resolve: {
            // .mjs needed for https://github.com/graphql/graphql-js/issues/1272
            extensions: ['*', '.mjs', '.js', '.vue', '.json']
        }
    }
}

@yangwao
Copy link

yangwao commented Jan 13, 2021

@jacogr has won the game! 🥳
❤️ Thank you very much! Probably, it will be demonstrated at today's Metaverse seminar 👯

image

@jacogr
Copy link
Member

jacogr commented Jan 13, 2021

Google is a friend :)

@polkadot-js-bot
Copy link

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.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators May 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants