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)

Most upvoted comments

Metro Bundler has encountered an error: While trying to resolve module react-native-notificationsfrom filewhatever, the package /project/ios/Pods/react-native-notifications/package.json was 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 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 under node_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:

diff --git a/node_modules/react-native-notifications/react-native-notifications.podspec b/node_modules/react-native-notifications/react-native-notifications.podspec
index c8e4e78..6db6a69 100644
--- a/node_modules/react-native-notifications/react-native-notifications.podspec
+++ b/node_modules/react-native-notifications/react-native-notifications.podspec
@@ -3,7 +3,7 @@ require 'json'
 package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
 
 Pod::Spec.new do |s|
-  s.name           = 'react-native-notifications'
+  s.name           = 'ReactNativeNotifications'
   s.version        = package['version']
   s.summary        = package['description']
   s.description    = package['description']
@@ -15,7 +15,7 @@ Pod::Spec.new do |s|
   s.requires_arc   = true
   s.platform       = :ios, '8.0'
 
-  s.preserve_paths = 'LICENSE', 'README.md', 'package.json', 'notification.ios.js', 'notification.android.js', 'index.android.js', 'index.ios.js'
+  s.preserve_paths = 'LICENSE', 'README.md', 'notification.ios.js', 'notification.android.js', 'index.android.js', 'index.ios.js'
   s.source_files   = 'lib/ios/*.{h,m}'
 
   s.dependency 'React'

Change ios/Podfile entry to:

  pod 'ReactNativeNotifications', :podspec => '../node_modules/react-native-notifications/react-native-notifications.podspec'
  • Run yarn install and cd 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 run npm pack in 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 adding files to 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.

@AlexD10S try this: pod 'react-native-notifications', :path => '../node_modules/react-native-notifications', it worked for me.

thanks, It works

@AlexD10S try this: pod 'react-native-notifications', :path => '../node_modules/react-native-notifications', it worked for me.