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

TypeError: Cannot read property 'getConstants' of null, #204

Open
0xtruly opened this issue Jan 5, 2023 · 38 comments
Open

TypeError: Cannot read property 'getConstants' of null, #204

0xtruly opened this issue Jan 5, 2023 · 38 comments

Comments

@0xtruly
Copy link

0xtruly commented Jan 5, 2023

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)
     };
    
@RonRadtke
Copy link
Owner

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.

@0xtruly
Copy link
Author

0xtruly commented Jan 6, 2023

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

@SheetJSDev
Copy link

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)

@0xtruly
Copy link
Author

0xtruly commented Jan 11, 2023

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

@RGLxAkuma
Copy link

@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

@SheetJSDev
Copy link

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

@RGLxAkuma
Copy link

RGLxAkuma commented Jan 23, 2023

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??

@SheetJSDev
Copy link

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 react-native-blob-util@0.17.0
echo "import RNFetchBlob from 'react-native-blob-util';" >> App.js 
npm run android

@SheetJSDev
Copy link

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

@RonRadtke
Copy link
Owner

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.

@SheetJSDev
Copy link

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 #204 (comment) :

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.

@RonRadtke
Copy link
Owner

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: #219

@WaheedRumaneh
Copy link

WaheedRumaneh commented Jun 6, 2023

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.

@SoyDiego
Copy link

SoyDiego commented Aug 2, 2023

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

Thanks

@Jheysson
Copy link

Same issue

@babarbahadur
Copy link

babarbahadur commented Aug 16, 2023

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

@sungsong88
Copy link

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

@atologistdipak
Copy link

same issue

@raldred
Copy link

raldred commented Oct 18, 2023

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",
    },
  },
}));

@zach-xing
Copy link

same issue

@Balthazar33
Copy link

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.

@Milanjiji
Copy link

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.

@buyi-web
Copy link

Same issue with Android

@m-sterspace
Copy link

Anyone have any fix for Android??

@zdsantos
Copy link

Same here

1 similar comment
@j-peace
Copy link

j-peace commented Jan 15, 2024

Same here

@m-sterspace
Copy link

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

@kirtikapadiya
Copy link

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.

@m-sterspace
Copy link

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.

@snitraM4500
Copy link

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 ?

@Favot
Copy link

Favot commented Feb 20, 2024

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

You can put this in the jest.config.js file, or create a specific file for it like __mocks__/react-native-blob-util.ts

Then update your jest.config.js as :

module.exports = {
  ...
    setupFilesAfterEnv: [
       ...
        './__mocks__/react-native-blob-util.ts',
    ],
};

@WaheedRumaneh
Copy link

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 ?

the setup file or you also can call it mock file, it's a custom file that you will create once set up a project, this file will be linked with jest.config.js and will contain all general packages mocks or any general test functions that you want to apply them on all test cases that you have within the project,

hope this could help you.

@mMarcos208
Copy link

Same issue on Android.
Any solution?

"react-native-blob-util": "0.19.4" and "react-native": "0.72.10",

@Rahmathirshad
Copy link

Rahmathirshad commented May 8, 2024

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.

I'm using expo, getting same error " TypeError: Cannot read property 'getConstants' of null, js engine: hermes ",...
I tried to follow steeps as above mentioned but I couldn't find "setup file", even tried to create as you said but there is no __test__ directory in my project....... what to do next please help..

even after building the app, It's throwing error and react-native-pad is not loading uri

@nicholas-masini
Copy link

Same issue here. I am using Expo 51. On iOS Development Build it works perfectly, however on Android it just crashes with this error.

@MahdiAsadifard
Copy link

Same issue on Android development build. Any solution?
"react-native-blob-util": "^0.19.9"
"react-native": "0.74.2"
"expo": "^51.0.9"

@Anurodhydv
Copy link

can anyone please provide the accurate solution to this ? react-native-blob-util is giving this error

@cheprasov-dev
Copy link

Same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests