Import from a shared folder from

I am creating a monorepo to store both my React web application and my React Native application, as they have a lot of common code.

The setup I'm trying to get is:

Project  
|  
+-- web  
+-- native  
----|------ file.js (should be able to import from 'shared/file2.js')  
+-- shared  
----|------ file2.js  
----|------ package.json (with name="shared")  
+-- package.json

From the following post: https://blog.callstack.io/a-cure-for-relative-requires-in-react-native-2b263cecf0f6#.4g1o5inru I added the package.json file to the shared folder with:

{ "name": "shared" }

and I run my native application from the .json package in the root folder, for example:

"start-native": "./native/node_modules/react-native/packager/packager.sh"

But for some reason, I get the following error: enter image description here

Any idea what I am doing wrong or how can I achieve importing files in my RN application from a public folder located upstream?

EDIT:
, .
, ( ...):

// in native/file.js  
import { aFunctionName } from 'shared/file2'

package.json(name = proj-root) :

import { aFunctionName } from 'proj-root/shared/file2'

!
Uri

+4
3

, @diegopartes.

, - .

:

Web
webpack.config.js . webpack.config.js , :

const path                = require('path');
const sharedPath          = path.join(__dirname, 'shared');

const config = {
...
resolve: {
    root: [sharedPath],
  },
...
}

. ,

- shared  
-- reducers  
-- actions  
--- todos.js

import { addTodo } from 'actions/todos'

, :

root, package.json { "name": "FOLDER_NAME" }. , , -:

- shared  
    -- reducers  
    -- actions  
    --- todos.js
    --- package.json

package.json:

{
  "name": "actions"
}

, -, .

node_modules, package.json - . webpack.config React Native, .

+2

- native, .

react-native init myProject, RN.

myProject  
|  
+-- android
+-- ios
+-- index.android.js
+-- index.ios.js
...

- .

myProject  
|  
+-- android
+-- ios
+-- index.android.js
+-- index.ios.js
+-- web
+-- shared
----|------ index.js 
...

/index.js

function hi() {
    return 'hi';
}

module.exports = {
    hi: hi
};

index.ios.js

import { hi } from './shared/'; 
+2

, node_modules?

, lerna, , , .

0

Source: https://habr.com/ru/post/1661562/


All Articles