2

I'm new in Node.js and React, so please excuse me if my question is not relevant. I have an app which uses Node.js(Express) server and React for UI. I have Classes which should be used both in server and UI. My root directory is like this

--root
----server
----client
----shared

But when I try to use the files of shared from client, I get an error Relative imports outside of src/ are not supported. I tried to use Lerna for common files, but ended up with the same error.

Can anybody advise how can I solve this? Any help is appreciated.

P.S. I already tried to eject and remove ModuleScopePlugin from webpack configuration file, however that added new errors and didn't solve the problem.

Arp
  • 146
  • 7
  • 1
    Possible duplicate of [The create-react-app imports restriction outside of src directory](https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory) – Chris Deacy Jun 01 '19 at 02:56
  • I'm curious, what do you need to do this for exactly? This kind of thing is mostly done in games for client prediction. – LJD Jun 01 '19 at 02:57
  • 1
    @JudeDesir - and also for shared validation code, for shared data structures etc. In fact, this is very common in compiled languages like C, C++, Java etc. where both server and client use the same shared libraries for data structures (in fact there are entire frameworks like CORBA and OLE that do this). It's only in web development where traditionally browser javascript has no access to shared libraries in other languages that we don't do this. Now with node we can and I've always believed we should – slebetman Jun 01 '19 at 03:05
  • 1
    @JudeDesir The ability to share common concerns without wrappers, api calls, etc. is one of the selling pints of JS on the server--it's quite handy. – Dave Newton Jun 01 '19 at 03:27
  • 1
    @JudeDesir Yes, I'm creating a game, where I need to keep users in server and also do calculations about them in UI. – Arp Jun 01 '19 at 03:35
  • in js sharing c-s code is named `isomorphic` or `universal` ... just use symlinks to make it virtually local? – xadm Jun 01 '19 at 08:21

0 Answers0