react-native-notifications: Error setting up in iOS (lib/dist/index.js doesn't exist)
The library works perfectly on Android, but in iOS when add the line:
pod 'react-native-notifications', :podspec => '../node_modules/react-native-notifications/react-native-notifications.podspec'
on the Podfile, install it and run the app, it crash because is unable to find the file lib/dist/index.js
See the full error:
Metro Bundler has encountered an error: While trying to resolve module react-native-notificationsfrom filewhatever, the package /project/ios/Pods/react-native-notifications/package.jsonwas successfully found. However, this package itself specifies amain module field that could not be resolved (/project/ios/Pods/react-native-notifications/lib/dist/index.js. Indeed, none of these files exist
The version is installing is: Installing react-native-notifications (3.1.4)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 3
- Comments: 19 (1 by maintainers)
The reason this happens is because the .podspec file includes package.json file, so it gets copied into
ios/Pods/react-native-notification. React-native Metro bundler finds that package.json file and uses it instead of the proper file (which is still undernode_modules).The solution for this specific problem is to delte the file
ios/Pods/react-native-notifications/package.json.This will raise another issue: the js code is looking for a dependency called ReactNativeNotifications, but the .podspec registers the package as react-native-notifications. The easier way is to change the podspec file to match the native module. I’m not sure about the implications of doing that when autolinking though.
If possible, the library could ship two podspecs: the current one for auto-linking; and an alternative for manual link with the native name ReactNativeNotifications.podspec
Workaround while the library is not fixed:
Use patch-package to change the podspec file in node_module so it doesn’t include package.json in the pod and also register the proper name.
After installing patch-package and adding it as postinstall, save this as patch file in
patches/react-native-notifications+3.1.4.patch:Change
ios/Podfileentry to:yarn installandcd ios && pod install@madandrija If you use a fork, you can build a dist folder using
npm run build. Then just add ‘dist’ folder to your repo and regenerate it after each js/ts code update. Or you can runnpm packin the react-native-notifications folder. It will generates a tgz file which you can upload as a binary to release in your github repo. More here https://glebbahmutov.com/blog/npm-install-with-just-github/ (just skip the part with addingfilesto package.json – it’s not needed for this lib)Thanks, doing this is not crashing. However, the handling methods of notifications: registerNotificationReceivedForeground, registerNotificationReceivedBackground… of the library are not working when receive a notification
I have this issue when trying to use a fork of the repo - the repo doesn’t contain
lib/dist😞 (which is included when retrieving the package from npm). Since there’s no dist, it doesn’t contain e.g. typescript definitions and the error message is totally valid (and the proposed solution doesn’t work for this case). Not sure how to generate this, but noting it down in case someone else uses a fork and runs into the same problem.thanks, It works
@AlexD10S try this:
pod 'react-native-notifications', :path => '../node_modules/react-native-notifications', it worked for me.