RonRadtke / react-native-blob-util

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
770 stars 130 forks source link

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

Open 0xtruly opened 1 year ago

0xtruly commented 1 year ago

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

RonRadtke commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

RGLxAkuma commented 1 year ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

RonRadtke commented 1 year ago

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 commented 1 year ago

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.

RonRadtke commented 1 year ago

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

WaheedRumaneh commented 1 year ago

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:

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 commented 1 year ago

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

Thanks

Jheysson commented 1 year ago

Same issue

babarbahadur commented 1 year ago

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 commented 1 year ago

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

atologistdipak commented 1 year ago

same issue

raldred commented 1 year ago

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 commented 12 months ago

same issue

Balthazar33 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

Same issue with Android

m-sterspace commented 10 months ago

Anyone have any fix for Android??

zdsantos commented 10 months ago

Same here

tiagopazhs commented 10 months ago

Same here

m-sterspace commented 10 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

Same issue on Android. Any solution?

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

Rahmathirshad commented 6 months ago

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 commented 5 months ago

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 commented 4 months ago

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