[React Native] import.meta is only valid inside modules error when importing initOnlyAsm

See original GitHub issue

After updating to the latest dependencies:

    "@polkadot/api": "^8.6.1",
    "@polkadot/keyring": "^9.3.1",
    "@polkadot/util": "^9.3.1",
    "@polkadot/util-crypto": "^9.3.1",
    "@polkadot/wasm-crypto": "^6.1.1",

and adding import '@polkadot/wasm-crypto/initOnlyAsm'; at the top of index.js file, I’m getting import.meta is only valid inside modules error for both iOS and Android.

Simulator Screen Shot - iPhone 13 - 2022-05-30 at 15 22 22

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
tzechuencommented, Jul 16, 2022

Hey all, I have discovered a workaround to get it working on Android. Basically, from my findings it seems that the WASM errors get thrown even if you insert Platform checking logic to prevent code execution i.e. doing if (Platform.OS === 'android') return; will still cause the WASM errors to get thrown, so I suspect it might have something to do with the bundling stages but I’m not entirely sure…

Today I discovered the workaround by chance, I remembered reading somewhere there was a way to do lazy imports so I did a quick research, tested and landed on this solution that worked for me.

At the top of the file you can have something like this:

import type { Keyring } from '@polkadot/keyring';
import { cryptoWaitReady } from '@polkadot/util-crypto';

let _Keyring: typeof Keyring | undefined = undefined;

cryptoWaitReady().then(() => {
  import('@polkadot/keyring').then((keyring) => {
    _Keyring = keyring.Keyring;
  });
});

Then in the code below:

      if (!_Keyring) {
        throw new Error('No Keyring available');
      }

      const keyring: Keyring = new _Keyring();

Then you can now use Keyring functions.

This solution might not work properly if you need to use PolkadotJS library within a constructor of a class. For use within a constructor, something like this should work:


  constructor() {
    cryptoWaitReady().then(() => {
      import('@polkadot/api').then((api) => {
        const { WsProvider, ApiPromise } = api;
        // Your other code here...
      });
    });
  }

If your IDE complains that the lazy imports above are not supported, you just need to add something like "module": "esnext" under "compilerOptions" inside tsconfig.json file (this is if you’re using TypeScript).

Also note I did not need to do initOnlyAsm as I initially did.

There are probably more elegant solutions but this seems suffice to get PolkadotJS working on Android for now. Please feel free to share if you find a more elegant way for this workaround. Would love to know. Thanks!

1reaction
saltictcommented, Jun 27, 2022

You don’t need the import explicitly when using RN since the wasm-crypto 6.1.1 release.

After install wasm-crypto 6.1.5 and clear react-native cache with react-native start --reset-cache.

Everything seem be ok like initOnlyAsm with some basic function like blake2AsHex and element like Identicon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS1343: The 'import.meta' meta-property is only allowed when ...
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports.
Read more >
How should we handle import.meta #6719 - GitHub
In webpack's use case, it'd be useful to put import.meta.hot in it. ... imports in webpack, but as only importing JS is the...
Read more >
import-meta-resolve - npm
Start using import-meta-resolve in your project by running `npm i import-meta-resolve`. There are 87 other projects in the npm registry ...
Read more >
Cannot Create Polkadot Keyring with React Native - ADocLib
[React Native] import.meta is only valid inside modules error when importing initOnlyAsm.Original URL.After updating to the latest dependencies: @polkadot/.
Read more >
Issue #417 (import.meta, Testing, React, Git/CLI)
The import.meta object currently exposes a single property: import.meta.url. This is the URL from which the module was loaded (in Node this is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found