I’m working on a full-stack application using React for the frontend and Express for the backend. Right now I have this folder setup:
project
├── client
| ├── node_modules
| ├── public
| ├── src
│ │ └── App.js
| ├── package.json
| └── tsconfig.json
├── server
| ├── node_modules
| ├── public
| ├── src
│ │ └── index.ts
| ├── package.json
| └── tsconfig.json
This looks clean to me because both parts have their own dependencies and configs. But now I need to share some utility functions and types between the client and server sides.
I tried creating a common folder at the root level with its own package.json and everything. On the server side I can import from it using path aliases like:
import { helperFunction } from @common/utils;
But for the React side (built with create-react-app), I would need to do:
import { helperFunction } from '../../common/src/utils';
The problem is create-react-app doesn’t allow imports from outside the src folder and I don’t want to eject because I’m not comfortable managing webpack configs myself.
Should I put the shared code inside the React src folder instead? Or is there a better way to structure this kind of project? I’m new to React and would love to know what the standard approach is for this situation.