react-native-blob-util icon indicating copy to clipboard operation
react-native-blob-util copied to clipboard

TypeError: Cannot read property 'getConstants' of null,

Open 0xtruly opened this issue 3 years ago • 41 comments

Hi, I am trying to convert an image url prefixed with https:// into base64 string using the react-native-blob-util, however I got this error after installing and also doing a pod install

  • react-native-blob-util Version: 0.17.0

  • RN Version: 0.70.4

       const handleShare = async () => {
         const meetupImageUrl = getImageUrl(meetup.cover_photo, ImageVariant.preview);
         await ReactNativeBlobUtil.fetch('GET', meetupImageUrl)
            .then((res) => { 
             let status = res.info().status;
              if (status == 200) {
                  // the conversion is done in native code
                  let base64Str = res.base64();
                  console.log('base64Str', base64Str);
              }
              else {
                  // handle other status codes
              }
          })
          // Something went wrong:
          .catch((errorMessage, statusCode) => {
              // error handling
          });
      console.log('meetupImageUrl', meetupImageUrl)
     };
    

0xtruly avatar Jan 05 '23 15:01 0xtruly

Did you install the package (and link) and rebuild the app and cleaned build caches first if needed? Sounds like it didn't load the lib correctly.

RonRadtke avatar Jan 05 '23 19:01 RonRadtke

Did you install the package (and link) and rebuild the app and cleaned build caches first if needed? Sounds like it didn't load the lib correctly.

Yes I took this exact steps

0xtruly avatar Jan 06 '23 12:01 0xtruly

To reproduce:

npx react-native init RNBU204 --version="0.70.6"
cd RNBU204
npm i --save react-native-blob-util
echo "import RNFetchBlob from 'react-native-blob-util';" >> App.js 
npm run ios

The Metro window shows the following errors:

 ERROR  TypeError: Cannot read property 'getConstants' of null, js engine: hermes
 ERROR  Invariant Violation: Failed to call into JavaScript module method AppRegistry.runApplication(). Module has not been registered as callable. Registered callable JavaScript modules (n = 10): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient.
        A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes

For iOS, the "fix" is to run pod install from the ios folder and rebuild the app. To see that in action, close Metro and run:

cd ios; pod install; cd ..
npm run ios

This probably should be documented somewhere, since the current documentation strongly suggests npm install suffices when working with RN 0.70 ("manual linking should not be required" and the actual react-native link command was removed)

SheetJSDev avatar Jan 07 '23 23:01 SheetJSDev

@SheetJSDev I'm going to try out your suggestions. Although I no longer need this package as I found another solution

0xtruly avatar Jan 11 '23 08:01 0xtruly

@SheetJSDev I'm going to try out your suggestions. Although I no longer need this package as I found another solution

What solution you found as I am getting the exact error when I tried this package

RGLxAkuma avatar Jan 23 '23 04:01 RGLxAkuma

For iOS, the "fix" is to run pod install from the ios folder and rebuild the app:

cd ios; pod install; cd ..
npm run ios

SheetJSDev avatar Jan 23 '23 04:01 SheetJSDev

For iOS, the "fix" is to run pod install from the ios folder and rebuild the app:

cd ios; pod install; cd ..
npm run ios

For android is there any solution??

RGLxAkuma avatar Jan 23 '23 04:01 RGLxAkuma

Our React Native + RNBU demo (https://docs.sheetjs.com/docs/demos/mobile/reactnative#demo select "RNBU" in step 4) builds and runs in the simulator for Android 12 (S) Platform 31 Pixel 5. This works in RN 0.70.6 + RNBU 0.17.1 and in RN 0.70.4 + RN 0.17.0 (the versions used by @dekunleroy).

Can you test if the same issue shows up in a new project? Try:

npx react-native init RNBU204 --version="0.70.4"
cd RNBU204
npm i --save [email protected]
echo "import RNFetchBlob from 'react-native-blob-util';" >> App.js 
npm run android

SheetJSDev avatar Jan 23 '23 19:01 SheetJSDev

Issue can be reproduced in Expo Snack (both iOS and Android). https://github.com/expo/snack/issues/385 tracking issue.

SheetJSDev avatar Jan 25 '23 08:01 SheetJSDev

Issue can be reproduced in Expo Snack (both iOS and Android). expo/snack#385 tracking issue.

Yeah but this is expo. I'm using native code and as far as I know am not part of the Expo SDK... So it won' work.

RonRadtke avatar Feb 24 '23 09:02 RonRadtke

Expo Snack issue was raised when trying to make our react native demo work with the snack. It happened to have the same message but

This issue is reproducible locally on iOS using the first snippet from an up-thread comment https://github.com/RonRadtke/react-native-blob-util/issues/204#issuecomment-1374646366 :

npx react-native init RNBU204 --version="0.70.6"
cd RNBU204
npm i --save react-native-blob-util
echo "import RNFetchBlob from 'react-native-blob-util';" >> App.js 
npm run ios

Tested on iPhone 14 (iOS 16.2), the error is

 ERROR  TypeError: Cannot read property 'getConstants' of null, js engine: hermes

The project issue is resolved by running pod install from the ios directory and rebuilding:

cd ios; pod install; cd ..
npm run ios

It's hard to tell if this is a bug in RNBU or merely an oversight in the docs. The Installation section of the docs does not mention the pod install step.

SheetJSDev avatar Feb 24 '23 09:02 SheetJSDev

I would add the pod install to the docs. It makes sense that a pod install might be required. But we had some issues on ios side too: https://github.com/RonRadtke/react-native-blob-util/pull/219

RonRadtke avatar Feb 24 '23 09:02 RonRadtke

if you face this issue when trying to test your code , put this code in setup file :

jest.mock('react-native-blob-util', () => { return { DocumentDir: () => {}, polyfill: () => {}, }; });

if you don't have the setup file you can add it like this:

  • create a new file setup/setup.test.ts inside your __test__ directory
  • link it with jest.config.js by adding this code setupFiles: ['<rootDir>/__tests__/setup/setup.test.ts'], testPathIgnorePatterns: [ '<rootDir>/__tests__/setup', ... ],
    • stop the metro if it was running and re-run the app again

now you can add any general mock like i mentioned at the beginning or test functions inside it to be applied on all your test cases.

WaheedRumaneh avatar Jun 06 '23 15:06 WaheedRumaneh

Same issue here when I'm trying to start the APP. Any solution?

Thanks

SoyDiego avatar Aug 02 '23 07:08 SoyDiego

Same issue

Jheysson avatar Aug 10 '23 00:08 Jheysson

I tried adding mock code into setup file.

jest.mock('react-native-blob-util', () => { return { DocumentDir: () => {}, polyfill: () => {}, }; }); Also downgraded the library to version 0.17.0 but still facing the same issue :(

TypeError: Cannot read properties of null (reading 'getConstants')

 at Object.<anonymous> (node_modules/react-native-blob-util/fs.js:14:39)
 at Object.<anonymous> (node_modules/react-native-blob-util/index.js:8:1)

My apps works totally fine but facing this issue while testing with Jest

babarbahadur avatar Aug 16 '23 11:08 babarbahadur

I have the same issue with Android. Any solutions? instrument.js cannot read property getConstants of null

sungsong88 avatar Sep 18 '23 14:09 sungsong88

same issue

atologistdipak avatar Oct 04 '23 11:10 atologistdipak

I was seeing this when running tests with jest. Sorted it by creating a manual mock __mocks__/react-native-blob-util.js containing simply:

export default jest.fn();

Which covered off tests which weren't actually touching react-native-blob-util. For tests of modules which did use react-native-blob-util, i mocked as follows:

jest.mock("react-native-blob-util", () => ({
  android: {
    actionViewIntent: jest.fn(),
  },
  ios: {
    openDocument: jest.fn(),
    previewDocument: jest.fn(),
  },
  config: jest.fn(),
  fs: {
    dirs: {
      DocumentDir: "yourdocumentdir",
      DownloadDir: "yourdownloadsdir",
    },
  },
}));

raldred avatar Oct 18 '23 14:10 raldred

same issue

zach-xing avatar Nov 20 '23 08:11 zach-xing

if you face this issue when trying to test your code , put this code in setup file :

jest.mock('react-native-blob-util', () => { return { DocumentDir: () => {}, polyfill: () => {}, }; });

The following worked for me:

jest.mock('react-native-blob-util', () => {
  return {getConstants: () => {}, polyfill: () => {}};
});

P.S. I faced this error only while running jest.

Balthazar33 avatar Nov 28 '23 12:11 Balthazar33

worked for me!

it was the issue of the version i was using 0.19.6 which was released about 2 weeks which was the problem i degraded the react-native-blob-util to version 0.19.5 and is working fine now.

Milanjiji avatar Dec 17 '23 10:12 Milanjiji

Same issue with Android

buyi-web avatar Dec 19 '23 10:12 buyi-web

Anyone have any fix for Android??

m-sterspace avatar Jan 03 '24 21:01 m-sterspace

Same here

zdsantos avatar Jan 11 '24 11:01 zdsantos

Same here

j-peace avatar Jan 15 '24 18:01 j-peace

In my case, I got these errors trying to use this with Expo Go, instead, you have to use an Expo Development build. You can create a dev build, sideload it onto your emulator / test device, and have it grab the latest js bundle automatically (just like expo go), by running npx expo run:android

m-sterspace avatar Jan 15 '24 19:01 m-sterspace

worked for me!

it was the issue of the version i was using 0.19.6 which was released about 2 weeks which was the problem i degraded the react-native-blob-util to version 0.19.5 and is working fine now.

not working with expo SDK 49 with expo go.

kirtikapadiya avatar Jan 30 '24 12:01 kirtikapadiya

worked for me!

it was the issue of the version i was using 0.19.6 which was released about 2 weeks which was the problem i degraded the react-native-blob-util to version 0.19.5 and is working fine now.

not working with expo SDK 49 with expo go.

It will not work with Expo Go, you need to use Expo Development Builds.

m-sterspace avatar Jan 30 '24 14:01 m-sterspace

if you face this issue when trying to test your code , put this code in setup file :

jest.mock('react-native-blob-util', () => { return { DocumentDir: () => {}, polyfill: () => {}, }; });

what do file do i put this information at ?

snitraM4500 avatar Feb 09 '24 06:02 snitraM4500