How to import existing Packages into the same directory in a Nodejs application

Asked

Viewed 112 times

0

Hello, I am creating a solution that has been distributed across multiple projects and that contains resources shared with each other. My solution is developed in general in Nodejs along with other solutions and so far has this structure:

<solution>
  | - .gitignore
  | - client   // ~> app em React UI para browser
  | - core     // -> artefatos comuns entre todos os projetos
  | - server   // ~> api
  | - database // ~> camada de DAO
  | - mobile   // ~> app em React Native para mobile
  | - desktop  // -> app em React Desktop para ser instalado localmente

Each directory will be a specific project (package.json) having its tests, src and dist. I need to import the package core in other projects so I can share classes, scripts and other elements. What I need to do to import the core into other packages.

1 answer

1


You need to ride a Monorepo.

The tools that will help you with this are as follows::

About the Yarn Workspaces

Yarn Workspaces serves to manage projects with multiple projects, its main features are:

  1. Install root dependencies for a main project and allow your sub-projects use it. This helps a lot to maintain updated shared dependencies and remove duplicate code from each project.
  2. Create symbolic links between dependencies to make them used among themselves.

About Lerna

Before Yarn Workspaces was released, Lerna was used for this type of architecture, which now, Yarn Workspaces does in a more "native" way by being directly in the package manager.

Now Lerna is used as an interface between Yarn Workspace and our projects, when executing some Lerna commands such as to link dependencies for example, it uses Workspaces from below the screens. In addition, Lerna also uses Git to help us solve some issues, such as publishing.

Speaking very briefly it executes commands in each of the projects at once, for example:

lerna publish

You’ll create a tag in Git by incrementing the version in package(s). json that has changed and publish in some NPM Registry

lerna run build

Will run Yarn build on all projects.

  • fantastic... that’s exactly what I was thinking of doing... I’m used to working with the "Solutions" that exist in c# projects and I would like to do it with nodejs... it makes development much easier besides a better organization, code control and a shovel of advantages ainds rsrs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.