Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share common typescript code across node projects

Assuming I have the following project structure:

  • webapps
    • ProjectA
      • SomeClass.ts
      • Package.json
    • ProjectB
      • SomeClass.ts
      • Package.json
    • Common
      • LoggingClass.ts
      • Package.json

The Common "LoggingClass" need to import a module from NPM. Let's assume that module is UUID (https://www.npmjs.com/package/uuid)

The problem, or the challenge is the following: We want to use and import the "LoggingClass" in both ProjectA and ProjectB.

Thing is, they can import the code from the common folder without a problem, but then the module "UUID" is not existent in those project because we did not specify so in their own respective Package.json.

I do not want to create actual node modules since my code needs to be checked into a git repository. (Some people recommend to develop directly into node_modules directory).

I would like to know what other fellow typescript developers do in the community to share their code when using npm. (If it was only typescript files it wouldn't be a problem, but because of the dependencies we have on npm.. it gets complicated).

Any thoughts would be greatly appreciated !

like image 427
Stacknerd Avatar asked Nov 27 '22 02:11

Stacknerd


1 Answers

I do not want to create actual node modules since my code needs to be checked into a git repository.

A real npm module is actually a way to go IMHO. You don't have to store it specifically in npmjs.org (you can have an own npm repository).

Some people recommend to develop directly into node_modules directory.

This is actually the most inconvenient way I can imagine of. Probably, I'm missing something important, but I'd love to know their way of thinking.

There are things you will not be able to [easily] achieve with git-only based solution. For example, how would you checkout a specific version of the shared code, with keeping the rest of the code intact? It is relatively easy when you have only one directory where all the shared code lives. But if you have many (basically, directories-as-packages) then it gets cumbersome. And even figuring out their "versions" is tricky too -- you'd have to invent and follow a git tagging rule or some other way to annotate commits.

Anyways, what I am trying to say is that my answer is not exactly what you asked for; my answer is rather a suggestion to reassess you position regarding (not) packing code into an npm module -- which is de facto a standard in the JavaScript/TypeScript community for a reason.

like image 197
Igor Soloydenko Avatar answered Nov 29 '22 05:11

Igor Soloydenko