react-native upgrade will change the path of MainActivity.java, how to stay with the original path? - react-native

I have cloned the f8app project
then run react-native upgrade in this project
It will create MainActivity.java at pathandroid/app/src/main/java/com/f8v2/MainActivity.java instead of use the original path.
the original path is
android/app/src/main/java/com/facebook/f8/MainActivity.java
below is my operation
$ git clone git#github.com:fbsamples/f8app.git
$ cd f8app
$ npm install
$ ls android/app/src/main/java/com/facebook/f8
MainActivity.java
$ react-native upgrade
Upgrading project to react-native v0.28.0
...
create android/app/src/main/java/com/f8v2/MainActivity.java
Additional Information
React Native version: 0.28.0+
Platform: android
Operating System: MacOS, Windows
actually it's not a problem of version.
It will create new files in other versions too, .e.g. 0.32.0
Looks like that it will create new files just based on the name in package.json instead of existing package name in android/app/src/main/AndroidManifest.xml, I have checked the code in upgrade process here https://github.com/facebook/react-native/blob/0.38-stable/local-cli/generator-android/index.js
this.option('package', {
desc: 'Package name for the application (com.example.app)',
type: String,
defaults: 'com.' + this.name.toLowerCase()
});
...
var javaPath = path.join.apply(
null,
['android', 'app', 'src', 'main', 'java'].concat(this.options.package.split('.'))
);
this.fs.copyTpl(
this.templatePath(path.join('package', '**')),
this.destinationPath(javaPath),
templateParams
);
how to stay with the original path?

Related

node-sass-middleware is not installing on windows

node-sass-middleware is not installing in my computer. I've tried almost every method to do so but nothing get hand.
It was saying to install latest version of python (I do), latest version of VS Code (I do) even after error is coming.
I've tried its alternative module node-sass but it does not match my need.
I want to setup this
const sassMiddleware = require('node-sass-middleware');
|
|
|
app.use(
sassMiddleware({
src: __dirname,
dest: __dirname,
debug: true,
outputStyle: 'extended',
prefix: '/css'
})
);
I've tried to install node-sass-middleware but its giving me this error :
see the error here from my terminal
continue error here
I found one way to use node-sass-middleware
Step1) download library from https://github.com/sass/node-sass-middleware
step2) extract it(if you download zip file) and now create an node-sass-middleware folder in node_module folder of your project
step3) copy all folders and files from an zip file(extract) and paste it inside node-sass-middleware folder(that we created in node_module FOLDER)
step4) now run your code in vs code for sass

Not finding relative modules on build when linked over "npm link xyz"

When running npm run serve, my VueJS projekt (Vue 2.6.11) running webpack is not finding my locally changed node module.
If i install it normally, it works (of course) and i see the changes i made in it, in the node modules folder of the project that linked it - so the link itself should be fine.
I linked it by using "npm link" in the project i want to link (i ran the build before) and then using "npm link [name]" in my main project.
The import i'm using in the main.js looks like this:
import [module-name] from '[module-name]'
just as it did when i had it installed regularly.
I also changed the settings of webpack by doing this:
module.exports = {
configureWebpack: {
resolve: {
symlinks: false //npm link
},
...
because the documentation of vue stated (for version 2, the new version has information about chainWebpack in it), that links won't work otherwhise as they're not resolved without it. still, this is the output on npm run serve:
This dependency was not found:
* [module-name] in ./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/views/CommentWriteArea.vue?vue&type=script&lang=js&
To install it, you can run: npm install --save [module-name]
(with the correct name in it of course)
Other solutions like installing the local version or forking the repository are not an option. It has to be linked for my use case

Autolinking dependencies in 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

Mojave + Xcode 10 build fails on glog config.h, gflags/gflags.h

I'm testing React Native 0.56.0-rc.2 on Mac OS Mojave and Xcode 10.
Running:
react-native init TestProject --version="0.56.0-rc.2"
cd TestProject
npm run start
react-native run-ios
Which generated some long errors.
Entering Xcode gave me a failure on missing config.h for glog, which I found could be built manually:
cd ./node_modules/react-native/third-party/glog-0.3.4
./configure && make && make install
Which passed that stage, but got me on yet another issue
(...)/node_modules/react-native/third-party/glog-0.3.4/src/glog/logging.h:85:10:
'gflags/gflags.h' file not found
I can't find anything related to RN and this on Google or Stack Overflow, only references to other packages and instructions to install those on a Debian-based system.
Is this a known issue?
Running this from the project directory fixed it for me:
cd ./node_modules/react-native/third-party/glog-0.3.4 && ../../scripts/ios-configure-glog.sh
This manually triggers the config script
A simple and quick resolution.
In Xcode, go to File->Project/Workspace settings.
Change the build system to Legacy Build system.
This is the resolution from Wesley's link. Saw something the other day about posting the direct answer and trying to avoid linking because links / websites can change. I was going to leave this as a comment, but don't have enough rep.
Update
It is a known issue, tracked here:
https://github.com/facebook/react-native/issues/19774
I resolved it by running following steps
In the root of the project, run npm install or yarn - install packages
cd node_modules/react-native - go to node modules directory
scripts/ios-install-third-party.sh - install # node_modules/react-native/third-party
cd third-party - go to newly created third party directory
cd glog-0.3.x - ls -la to find your directory version number or just use tab to auto-complete)
./configure - run setup
cd ../../../../ - change back to your project directory
react-native run-ios or react-native run-android - deploy
If any of these solution does not work, please check your project path. Project path and/or directory names should not contain any
space in its name or you can create project on Desktop or in Documents
directory.
Because of invalid name in project path, React Native project unable to link / add glob header files inside project / workspace.

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!