Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Share code between my client and my server folders

I have a directory that contains two folder : my server app (NodeJS) and my client app (ReactJS).

My directory structure looks like this :

root/
|- client/
   |- package.json
   |- src/
      |- ...
|- server/
   |- package.json
   |- src/
      |- ...

How can I add a common folder at the root of my directory to share enums and utils with both my server and my client ?

Thanks!

like image 729
nsayer Avatar asked Sep 10 '25 21:09

nsayer


1 Answers

You have multiple solutions:

  • create a folder common under root and just require the files you need from your files. But you could end up with "long" require such as require("../../../../../common/file")

  • use module-alias to avoid that problem: https://github.com/ilearnio/module-alias

  • you could make common a local module (using file:) and install it in package.json https://docs.npmjs.com/files/package.json#local-paths

{
  "name": "common",
  "dependencies": {
    "common": "file:../common"
  }
}

Then you can just require what you need easily

like image 160
David Avatar answered Sep 13 '25 10:09

David