Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local require() paths for React-Native

I am looking for a convenient way to access files in the root of my application while avoiding require() strings that look like:

require('../../../../myModule')

There are some good solutions out there for Node (https://gist.github.com/branneman/8048520) but I haven't seen a way to use global variables in React Native.

Does anyone have a clean solution to this problem?

like image 401
hoodsy Avatar asked May 04 '15 21:05

hoodsy


People also ask

Can I use require in react native?

Yes the latest React Native tutorials and examples use the new import syntax. I think most people prefer the new ES6 syntax. However no JS engines implement ES6 modules currently, so it needs to be converted by an ES6 transpiler (e.g. Babel) to require statements.

How do you get the path in react native?

The most convenient way : Use react-native-document-picker, on selection it will give you a Relative path, something like this content://com.android...... . Pass that Relative path to Stat(filepath) function of the react-native-fetch-blob library. The object will return absolute path.

How do I set dynamic image path in react native?

React Native doesn't deal with dynamic images, only static images. Therefore, you have to front up all the images – you cannot construct the name and path dynamically.


4 Answers

From Marc Shilling's answer on https://github.com/facebook/react-native/issues/3099

You can use an absolute path on imports/requires:

import {ProximaNovaText} from 'MyApp/src/components'; require('MyApp/src/utils/moment-twitter');

where 'MyApp' is whatever name you registered in your index.ios.js file

Note for VS Code: This works, but be warned that you might lose intellisense and cmd/ctrl + click. Thanks Johan for the info about CS code

like image 72
Tiagojdferreira Avatar answered Oct 19 '22 23:10

Tiagojdferreira


You can mark a directory as a package by adding a package.json within the root directory you want to resolve.

e.g:

- app
    - package.json     // ← Add this package.json file
    - config
    - components
    - ... (etc)

package.json should look like this:

{ "name": "app" }

Restart your packager

react-native start --reset-cache

Now you can use the following in all of you project files:

import store from 'app/config/store';
import Button from 'app/components/Button';

You can use this same method across other directories in your project, I don't think this works via require, although image paths seemed work.


As noted in the comments, you may lose auto-complete in your editor (VSCode).

For Jetbrains IDE's there are some ongoing tickets here:

  • https://youtrack.jetbrains.com/issue/WEB-17254
  • https://youtrack.jetbrains.com/issue/WEB-20104#comment=27-1486526
  • https://intellij-support.jetbrains.com/hc/en-us/community/posts/205434510-Configure-custom-modules-resolve-folder

This might help with Jetbrains IDE's in the meantime.

// A slash may allow auto-complete to work in your IDE (but will fail to resolve)
import Button from '/app/components/Button'; // Cannot be resolved
like image 29
Anil Avatar answered Oct 20 '22 00:10

Anil


Put code below on top of your myModule file:

/**
 * @providesModule myModule
 */

Then you can use require('myModule') in any other files.

like image 26
DoanND Avatar answered Oct 20 '22 00:10

DoanND


Complementing @Tiagojdferreira

You can use his solution with babel-plugin-module-resolver library.

Install with:

npm install --save-dev babel-plugin-module-resolver

Configure .babelrc adding plugins property like this:

{
  "presets": ["react-native"],
  "plugins": [
    ["module-resolver", {
      "alias": {
        "@src": "MyApp/src",
        "@otherAlias": "MyApp/src/other/path",
      }
    }]
  ]
}

Usage:

require('@src/utils/moment-twitter');

Hope this helps!

like image 22
pedro.goes Avatar answered Oct 20 '22 01:10

pedro.goes