Reputation: 15
I have 3 npm projects, all exists under the same root folder. The projects are more complex than the example below (using webpack, JS frameworks, etc.) but to keep it simple for illustration purposes this is the structure:
root
├── root_index.js
├── package.json
├── project_1
│ ├── index.js
│ └── package.json
├── project_2
│ ├── index.js
│ └── package.json
└── project_3
├── index.js
└── package.json
My problem is I find lots of situations in the code where imports, for example from project_1 to project_2 contains lots of '..'
, for example - '../../../../project_2/some_module'
.
Would be nice if I could define a global variable '@project_2'
that will point to the project and then I could change my imports to '@project_2/some_module'
.
I saw webpack offers some solutions for aliases but I don't use webpack in my root folder, only in each project individually and so I cant use its configuration to control all projects from a single point. I also found this 'module-alias' package but it didn't work properly together with the configurations (mostly webpack) I have in each of the projects.
any idea how can I define these global variables so I could use them in each of the projects?
Upvotes: 1
Views: 1067
Reputation: 253
It sounds like you want to set up your project as a mono-repo.
I would recommend you to use the yarn workspaces concept and Lerna to help you manage it.
What is mono-repo? In short, a so-called Mono-Repo is a (git) repository that houses multiple projects. Such projects are called workspaces or packages.
Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm. checkout official website: https://lerna.js.org/
Attaching a good article to help you start with yarn workspaces and lerna: https://medium.com/hy-vee-engineering/creating-a-monorepo-with-lerna-yarn-workspaces-cf163908965d
Upvotes: 1