Autolinking dependencies in React Native - react-native

Context
I have a couple of React Native apps which share some components and helpers.
So I created a library to store the shared code, and I use it in my apps by adding it as a dependency in the respective package.json.
Inside my custom library, I use react-native.config.js to link some fonts
module.exports = {
assets: ['./assets/fonts']
};
And in my apps, I do npx react-native link my-custom-library to link these fonts.
Now, this shared library is also using react-native-community/react-native-localize.
And I don't know how to link it in my apps 🤔
I have 2 questions here:
Why do I need to do npx react-native link my-custom-library for the fonts?
Shouldn't React Native 60's autolinking feature do this for me automatically?
How can link react-native-localize using this setup?

1) About the fonts for RN 0.60+:
you should have a react-native.config.js
with a content like this:
module.exports = {
project: {
ios: {},
android: {},
assets: ['./src/assets/fonts', 'react-native-vector-icons'],
// Add the rn-vector icons in the above array only if you use that module, or your <libraryName> too.
};
Run react-native link and you should see a log like this:
info Linking assets to ios project info Linking assets to android
project success Assets have been successfully linked to your project
Add the fonts in Info.plist file:
<key>UIAppFonts</key>
<array>
<string>AntDesign.ttf</string>
<string>Entypo.ttf</string>
<string>EvilIcons.ttf</string>
<string>Feather.ttf</string>
<string>FontAwesome.ttf</string>
<string>FontAwesome5_Brands.ttf</string>
<string>FontAwesome5_Regular.ttf</string>
<string>FontAwesome5_Solid.ttf</string>
<string>Foundation.ttf</string>
<string>Ionicons.ttf</string>
<string>MaterialIcons.ttf</string>
<string>MaterialCommunityIcons.ttf</string>
<string>SimpleLineIcons.ttf</string>
<string>Octicons.ttf</string>
<string>Zocial.ttf</string>
</array>
Check Run Script Only When Installing in Xcode->YourProjectTarget->BuildPhases-> Copy Pods Resources
Make sure you clean all the cache:
rm -rf $TMPDIR/react- && rm -rf $TMPDIR/metro- && rm -rf $TMPDIR/haste-* && watchman watch-del-all && rm -rf ios/build and restart JS server.
Source and more info here:
https://medium.com/#mehran.khan/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4
I couldn't make the fonts work without the react-native link command and it seems that other people encountered this issue too.
2) About the react-native-localize
If you already linked it manually just run react-native unlink react-native-localize and then go to ios folder and run a pod install.
The line use_native_modules (from Podfile) will make the pod installer search for a pod spec file (RNLocalize.podspec) in the library folder from node_modules and install it and link it automatically.
Notes:
This answer has been tested in RN 0.61.4 on iOS simulator
Hints how to link a library that provides fonts I found here:
https://github.com/oblador/react-native-vector-icons/issues/1041

Related

How to run pod file in iOS directory in react native?

error Could not find "Podfile.lock" at projects\reactNative\ios\Podfile.lock. Did you run "pod install" in iOS directory?
(node:11696) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use node --trace-warnings ... to show where the warning was created)
error Could not get the simulator list from Xcode. Please open Xcode and try running project directly from there to resolve the remaining issues. Run CLI with --verbose flag
Issue 1
For fixing the issue:
Could not find "Podfile.lock" at projects\reactNative\ios\Podfile.lock. Did you run "pod install" in iOS directory?
Please make sure you have install cocoapods and run this command at your project root:
gem install cocoapods // If you haven't install cocoapods
cd ios && pod install && cd ..
Issue 2
For fixing the issue:
(node:11696) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency (Use node --trace-warnings ... to show where the warning was created)
Please downgrade your node.js version to v13.x.x, one of the solutions below:
npm install -g n
sudo n 13.13.0
Issue 3
For fixing the issue:
Could not get the simulator list from Xcode. Please open Xcode and try running project directly from there to resolve the remaining issues. Run CLI with --verbose flag
Please make sure you have installed Xcode correctly from App Store and use the latest React Native version in your project (should be v0.62.2 now for stable). If still cannot open the project, please try to add some simulator in Xcode's developer settings.
Cheers!
you did not install cocoapods
install cocoapods commands
sudo gem install cocoapods
after that in react native directory
cd ios && pod install && cd ../ && react-native run-ios
CocoaPods manages library dependencies for your Xcode projects. We need it to setup the iOS project. Like yarn
sudo gem install cocoapods
The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects. Like package.json
access to the folder with Podfile, run
pod install
This will resolve the code of those dependency framework, and generate Pods.xcodeproj for your iOS project.

Could not find the following native modules after upgrading the react-native to latest version 0.61.4

After upgrading the react-native version I am getting the following. And failed to run the build in simulator
After this, I have run react-native run-ios it shows all my packages are manually linked and you need to unlink all packages.
Also, can anyone clarify the react-native-config.js file is mandatory for this after updating to the latest react-native.
Always I open the app.Xcodeproj but the below shows I need to open app.xcworkspace. Can anyone clarify this?
warn The following packages use deprecated "rnpm" config that will stop working from next release:
- react-native-fetch-blob: https://github.com/wkh237/react-native-fetch-blob#readme
- react-native-orientation: https://github.com/yamill/react-native-orientation#readme
- rn-fetch-blob: https://github.com/joltup/rn-fetch-blob#readme
Please notify their maintainers about it. You can find more details at https://github.com/react-native-community/cli/blob/master/docs/configuration.md#migration-guide.
error Could not find the following native modules: BugsnagReactNative, react-native-camera, react-native-cookies, react-native-fast-image, react-native-fetch-blob, RNFS, RNGestureHandler, RNImageCropPicker, react-native-image-picker, RNImageRotate, BVLinearGradient, react-native-orientation, RNShare, RNSVG, RNVectorIcons, RNViewShot, react-native-webview, rn-fetch-blob. Did you forget to run "pod install" ?
info Found Xcode workspace "Fashion.xcworkspace"
This should fix your issue.
rm -rf ios/Pods && rm -rf ios/build && cd ios && pod install && cd ../
rm -rf node_modules && rm yarn.lock && yarn install
PS: In react-native version 0.60 and above, native modules are now auto-linked. See the docs for more: https://facebook.github.io/react-native/blog/2019/07/03/version-60#native-modules-are-now-autolinked
firstly, when you update the react-native version, you should react-native official document. to avoid some wrong things. the details for update in the react-native update helper.
and for your question, since 0.60, CocoaPods are now part of React Native's iOS project. If you weren't already, be sure to open iOS platform code using the xcworkspace file from now on.
At the same time, a react-native link is now not needed in most situations anymore. the react-native team overhauled the linking process in general. Be sure to react-native unlink any preexisting dependencies as mentioned in the docs above.for more details, you can read this article.
for the rnpm problem, firstly you should delete it in the package.json.
then create a react-native.config.js file in project folder.
const ios = require('#react-native-community/cli-platform-ios');
const android = require('#react-native-community/cli-platform-android');
module.exports = {
project: {
ios: {},
android: {},
},
// assets: ['Resources/fonts'], // stays the same
// commands: require('./path-to-commands.js'),
};
then in the rn-fetch-blob(make is as an example), you also create
react-native.config.js and modify it according to its rnpm config
In the latest react-native stable version, you dont need to link the 3rd party libraries, just install it and it will work fine
Firstly,
So, when you are migrating from lower version to higher stable version,
then what you need to do, just unlink all the libraries you have used
in your project as react-native automatically detects its
dependencies. and moreover remember that whenever you install such
libaries you don't need to run the react-native link <library-name>
to link it.
Secondly,
Now you need to work with xcworkspace file as in the latest version,
ios are dealing with pods, and when pods comes into picture then you
have to use xcworkspace instead of Xcodeproj in your ios project
You can check the changelog here for react native 0.61.4 version.
I hope this helps....Thanks :)
My solution is to combine some tips above and update mine Pod file
Simply reset:
rm -rf ios/Pods && rm -rf ios/build
cd ios && pod install && cd ..
rm -rf node_modules && rm yarn.lock && yarn install
Reason is to maximize future compatibility.
Here is a facebook provided template, pick some goodies for your existing old pod file:
https://github.com/facebook/react-native/blob/0.63-stable/template/ios/Podfile
You may found code below is auto linking,
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/#react-native-community/cli-platform-ios/native_modules'
You may need to upgrade ios target
platform :ios, '10.0'
You may use Flipper without use_frameworks! enabled
Could be that there is something in your podfile that causes this. For example, I removed a library from my project but there was still a reference to it in the podfile:
pod 'react-native-onesignal', :path => '../node_modules/react-native-onesignal'
Once commenting/removing this line, pod install works great.
For me just converting npm install to yarn install worked because other team was using yarn and i was trying to install packages using npm so it created some conflict.
I recloned the project then
'yarn install', 'react-native link' and then
'pod install'

BVLinearGradient was not found in the UIManager?

There is documentation about this, but it is for macOs only. https://github.com/react-native-community/react-native-linear-gradient.
I linked the library by running npm install react-native link react-native-linear-gradient.
I don't understand this i dont have a ios folder, i installed pod but my ios folder remained empty:
iOS
For React Native >= 0.60.0 run the following command in the ios/ folder and skip the rest of the section.
pod install
CocoaPods
Add the following line to your Podfile:
pod 'BVLinearGradient', :path => '../node_modules/react-native-linear-gradient'
or:
This below is only for macOs ?
Manually
Open your project in Xcode, right click on Libraries and click Add Files to "Your Project Name" Look under node_modules/react-native-linear-gradient/ios and add BVLinearGradient.xcodeproj. (Screenshot 1) (Screenshot 2).
Add libBVLinearGradient.a to Build Phases -> Link Binary With Libraries (Screenshot 1) (Screenshot 2).
Click on BVLinearGradient.xcodeproj in Libraries and go the Build Settings tab. Double click the text to the right of Header Search Paths and verify that it has $(SRCROOT)/../react-native/React - if it isn't, then add it. This is so Xcode is able to find the headers that the BVLinearGradient source files are referring to by pointing to the header files installed within the react-native node_modules directory. (Screenshot).
How to do it in Windows and i don't have an ios map inside node_modules/react-native-linear-gradient/?
how can i make it work?
Thanks for your time
use this one if you are using expo
1 . paste this in terminal window :
expo install expo-linear-gradient
2.add the import statement to the file :
import {LinearGradient} from 'expo-linear-gradient';
worked for me !
Check whether these two lines are in your 'package file'.
'React', :path => '../node_modules/react-native'
'BVLinearGradient', :path =>
'../node_modules/react-native-linear-gradient'
If yes then --
Run "pod install" under $project path/ios before react-native run-ios.
Cheers!!
Easy Way..! Try This..!
Run this code in Terminal window
expo install expo-linear-gradient
Use this code to import the LinearGradient
import { LinearGradient } from "expo-linear-gradient";
I had similar issue with Android.
Solution: Kill the app and relaunch this should solve the problem
First, install the https://github.com/react-native-community/react-native-linear-gradient library.
then go to the cd ios and run
cd ios
pod install
now use go back using cd ..
and run (optional )
react-native link react-native-linear-gradient
now run
react-native run-ios
Simply you just need to go to ios folder
cd ios
pod install
That's it
If using expo and this error is thrown after installing react-native-linear-gradient follow along.
error ? the expo framework still does not have "sight" of the linear gradient package .
solution ? Run the command "expo install expo-linear-gradient" from your root folder, go to the file that you had previously imported the linear gradient as "import LinearGradient from 'react-native-linear-gradient" and change the line to "import {LinerGradient} from 'expo-linear-gradient'".
Restart your server, "expo start".
PS: This solution is only applicable for react-native projects being built using expo
if you're using windows & react-native cli (not expo):
1- in android/settings.gradle, check if
include ':react-native-linear-gradient'
project(':react-native-linear-gradient').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-linear-gradient/android')
exist .. if not, add them
2- in android/app/build.gradle, check if
implementation project(':react-native-linear-gradient')
exists in dependecies{ ... } .. if not, add it
3- in android/app/src/main/java/com/{YOUR_APP_NAME}/MainApplication.java , find
#Override
protected List getPackages() { ....}
Then replace the comment // packages.add(new MyReactNativePackage());
with =>
packages.add(new LinearGradientPackage());
Android
If you are on android, simply do the native rebuild again with:
yarn android
or
npm run android

undefined is not an object (evaluating 'RNGestureHandlerModule.State'

I have installed react-navigation in my React Native project. Its a starter project doesn't have any codes. But while running project I am facing error like this.
Here is my Navigation code
import { createStackNavigator } from 'react-navigation';
import Home from './screens/Home';
import WeatherDetail from './screens/WeatherDetail';
const Navigation = createStackNavigator({
Home: { screen: Home },
WeatherDetail: {
screen: WeatherDetail
}
});
export default Navigation;
And here is the App.js codes
import Navigator from './Router';
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Navigator />
</View>
);
}
}
If I remove the navigator component from the App.js and replace it with a Text the application runs without any error.
remove node_modules and package-lock.json
npm install
npm install --save react-navigation
npm install --save react-native-gesture-handler
react-native link
If you are using react-link to link your dependencies:
0pen your ios Podfile and delete all yout linked dependencies: pod 'xxxxxxx', :path => '../node_modules/xxxxx
Close Xcode
In your /ios folder run "pod update"
In your project source run "react-native link"
Open Xcode and Clean Build Folder from Xcode Menu -> Product
Run your application from Xcode
Link manually the dependency "react-native-gesture-handler" into your Xcode Project following steps in documentation: https://facebook.github.io/react-native/docs/linking-libraries-ios
Now run your application from Xcode, you should be fine.
From the official doc:
If you're on React Native >= 0.60, you need to disable autolinking for react-native-gesture-handler first. To disable autolinking for it, create a react-native.config.js file in the root of your project with the following content:
module.exports = {
dependencies: {
'react-native-gesture-handler': {
platforms: {
android: null,
ios: null,
},
},
},
};
If your are using React 0.60, just omit this official doc. Follow following steps:
rm react-native.config.js if exist
react-native link react-native-gesture-handler
cd ios && pod install && cd ..
react-native run-ios
I get the same error on ios from RN v0.60
The following worked for me:
cd ios
pod install
First, remove node_modules and package-lock.json and run npm install.
After installing the react-navigation package in your React Native project. You should install react-native-gesture-handler. If you’re using Expo you don’t need to do anything here, it’s included in the SDK. Otherwise:
npm install react-native-gesture-handler
And finally, link gesture dependency as:
react-native link react-native-gesture-handler
This answer based on the React Navigation document.
maybe someone come here because same problem as me.
i got this error because i'm using react-navigation version 3.x, in that version stackNavigator is change to createStackNavigator and should use createAppContainer(createStackNavigator)
im fix it just like mr.amiri said but im not delete my node_module im just follow step 3 - 5
Since I am not allowed to comment, I am posting it here. This is the answer by #Amiri Houssem but I am adding one more thing:
remove node_modules and package-lock.json
npm install
npm install --save react-navigation
npm install --save react-native-gesture-handler
react-native link
If there is an error even after these 5 steps, check android/settings.gradle and change that line to this:
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
I'm answering because none of the above answers were relevant to me.
I got this error because VSCode automatically inserted import { TouchableOpacity } from 'react-native-gesture-handler' as a dependency in my file when I added a <TouchableOpacity> element.
Give the last files you edited a once-over just in case there's an import statement you don't expect!
I used this in the CLI to solve the issue
cd iOS
pod install
cd ..
react-native unlink react-native-gesture-handler
Uninstalled the app from the device and run the android project - react-native run-android and it worked fine
you can check your version of react-native
if React Native 0.59 and lower you can
react-native link react-native-gesture-handler
and check your #react-navigation version in
https://reactnavigation.org/versions
may be its late.
Temporary solution downgrade the version of react navigation:
1- unlink and uninstall react-navigation and the handler
2- add "react-navigation": "^2.18.2" to package.json
3- remove node_modules folder
4- npm i
5- react-native link
if you have done the above, it hasn't run yet, try this
if you use Windows
cd android
.\gradlew cleanBuildCache
and try running it
react-native run-android
Even though react-native-gesture-handler is present in node_modules folder, we need to add it to path or install it again. Then link with native code.
1) npm install --save react-native-gesture-handler
success Saved 1 new dependency.
info Direct dependencies
└─ react-native-gesture-handler#1.0.15
info All dependencies
└─ react-native-gesture-handler#1.0.15
2) react-native link
rnpm-install info Linking react-native-gesture-handler ios dependency
rnpm-install info Platform 'ios' module react-native-gesture-handler has been
successfully linked
rnpm-install info Linking react-native-gesture-handler android dependency
rnpm-install info Platform 'android' module react-native-gesture-handler has
been successfully linked
3) react-native run-android or react-native run-ios
If you are using a configuration with Podfile in ios. Follow this steps:
Comment this line pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler' in <react-native-project>/ios/Podfile.
Open <react-native-project>.xcworkspace Add RNGestureHandler.xcodeproj in Libraries (Right click over library folder and select "Add files to ") from node_modules/react-native-gesture-handler/ios.
Add libRNGestureHandler.a in (Build Phases) -> (Link Binary With Libraries).
This worked well for me as well.
1) npm install react-navigation
2) npm install react-native-gesture-handler
3) npm intstall
4) react-native link
uninstall the app
5) react-native run-android
Execute the following commands in command prompt (Run as Administrator)
npm install react-navigation
npm install react-native-gesture-handler
npm intstall
react-native link
Re-Install the application will resolve the issue.
I struggled with this and none of the above answers worked for me. Here's what I had to do:
Delete your ios folder entirely.
Delete your node_modules folder and package-lock.json.
Restart your computer.
Run react-native eject to re-create your native code folders.
Run npm install
Run react-native link
Run npm run start -- --reset--cache
Now run react-native run-ios
see your native react version, if version 0.60 then you must migrate to androidX using a jetifier, follow the steps in this link
https://github.com/mikehardy/jetifier
successful for me :)
If you're on React Native >= 0.60:
You should see the following entry in your ios/Podfile:
pod 'RNGestureHandler', :path => '../node_modules/react-native-gesture-handler'
If this line is not there, first run:
react-native link react-native-gesture-handler
ios/Podfile should now contain a new entry.
Now run this command from iOS directory /ios/:
pod install
Run your iOS project again either directly using XCode or react-native run-ios

React Native: npm link local dependency, unable to resolve module

I am developing a button ui package for react native. I try to build an example project to test this button. The directory structure is as follows:
my-button/
package.json
index.js
example/
package.json
index.js
I try to use npm link:
cd my-button
npm link
cd example
npm link my-button
In example/node_modules/ I can see my-button symlink, VSCode also can auto complete function in my-button package.
But execute example app will show error:
Unable to resolve module my-button ...
Module does not exist in the module map or in these directories: ...
But the path in the error message is correct.
Don't know where I was wrong, or in React-Native have any special way to deal with link local dependency?
I also tried npm install file:../.. It works fine in this way, but not easy to update dependency in example/ after I edited my-button.
The npm link command doesn't work because React Native packager doesn't support symlinks.
After a little research, I discovered that there are two ways to go about it.
Use haul packager in the example app. Haul supports symlinks, so you can use npm link as usual.
Use local dependency via file:../ and then edit files in node_modules folder or reinstall every time you make changes.
I found Haul to work great for this use-case and even set-up a little starter project that also includes storybook, which is really helpful if you have many components to switch between.
Try wml (https://github.com/wix/wml)
It's an alternative to npm link that actually copies changed files from source to destination folders
# add the link to wml using `wml add <src> <dest>`
wml add ~/my-package ~/main-project/node_modules/my-package
# start watching all links added
wml start
I couldn't always make it work with yarn link. What i found extra useful is yalc:
First install it globally once forever:
npm install -g yalc
In the local library/package (i'll call it my-local-package), and run:
yalc publish
Then in your project which uses my-local-package as a dependency, run:
(if you already have added it with any other way, first uninstall it (npm uninstall -S my-lockal-package)
yalc add my-local-package
npm install
If my-local-package is a native module, then run react-native run-android to link the dependency. (or run-ios)
If you make any change in the my-lockal-package, then:
cd path/of/my-local-package
yalc push //updates the local package
cd path/to/my-project
npm install
react-native run-android (or run-ios)
In case the update hasn't been applied, try to cd android && ./gradlew clean && cd .. and then rerun: react-native run-android.
I'm having the same issue while developing a native module wrapper around an existing native SDK. At first I followed #aayush-shrestha's suggestion to install the package locally. Like this:
npm install ../<package-folder> --save
This works as long as I reference the module via NativeModules. Import it:
import { NativeModules } from 'react-native';
And then access a module called ActualModuleName like this:
NativeModules.ActualModuleName
But it fails when I attempt to import the module by name:
import { ActualModuleName } from 'react-native-actualmodulename'
To make that work I had to first pack the package. Run this in the package's root directory:
npm pack
This generates a gzipped tarball:
react-native-actualmodulename-1.0.0.tgz
Now install that in your app:
npm install <path/to>/react-native-actualmodulename-1.0.0.tgz
An enormous downside to this is that you have to re-pack the package every time you make a change to the module. The only workaround I know of is to modify the package's files in node_modules directly and then copy those changes back to your repo when you're done.
But the upside is that your app's source can import ActualModuleName the same way you'll import it once it's released via npm; no environment-specific code necessary.
You can use npm link using Metro. Just add the source folder of the linked package to watchFolders in your metro.config.js.
Ran into the same problem. While I could not make npm link work as it should, I worked around it by installing the local package in the project folder
npm install ../<package-folder> --save
This will install the package like a regular package but from the local folder.
The downside is that the changes you make on the package will not be reflected. You will have to npm install after every change.
Change your package.json
//...
"dependencies": {
//...
"my-button" : "file:../"
},
//...
I also came across this problem. After visiting the below link, I came to know that react- native does not support symlinks.[Click here][1]
However, I have solved this by adding these lines in the metro.config.js file. Please replace your_module_name with your module name.
const path = require('path');
const thirdPartyPath = path.resolve(__dirname + '/../your_module_name/'); // Path of your local module
const thirdParty= {
'your_module_name': thirdPartyPath,
};
const watchFolders = [ thirdPartyPath];
module.exports = {
// existing dependencies
resolver: {
thirdParty,
},
watchFolders
};
I ran into the same problem.
I tried to install a local module using npm, and kept running into the issue of not being able to resolve the module, even though I could see the folder in node_modules and autocomplete of class and method names worked.
I was able to bypass it by installing the local library using yarn instead of npm after seeing this open issue on github. Issue was opened September 2020 and no comment from Facebook as of yet.
This work for me:
step 1 go to package:
npm link packageNameHere
This will link this package to global node_module
step 2 go to directory which you want to use this package and run these
npm link pathToPackageDirectory
npm install pathToPackageDirectory
ex: npm link ~/myDemoPackage
This will link global node_moudle to this project
If you want to import package to file, USE FILE PATH INSTEAD OF PACKAGE NAME !
ex:
my package name is stripe-api-helper. my code are in src/index.ts
then I need to resolve like this:
import { postStripe, Item } from '#aliciaForDemo/stripe-api-helper/src'
if u use '#aliciaForDemo/stripe-api-helper' it will fail.
Could never get my own environment working using any other suggestions, but found a hack that works well (though not ideal) that can be easily set up in just a few lines of code and without changing your RN project configuration.
Use fs.watch for changes recursively in the directory where you're working on your library, and copy the updates over whenever there's been a change:
import fs from 'fs'
const srcDir = `./your-library-directory`
const destDir = `../your-destination-directory`
fs.watch("./src/", {recursive: true}, () => {
console.log('copying...')
fs.cp(srcDir, destDir, { overwrite: true, recursive: true }, function() {
console.log('copied')
})
})
For those still looking for a simple solution without other dependency, try this:
yarn --version
1.21.1
npm --version
6.13.4
Install in project root
cd my-button
yarn install or npm install
register linking in my-button
yarn link or npm link
Install example project
cd example
yarn add ../ or npm add ../
link to my-button
yarn link my-button or npm link my-button
complete pod installation (if necessary)
cd ios
pod install
Try to run
npm run watch
inside the button package. Currently, I'm using this to apply changes from the library to my main project. Please let me know if it works!