Share common typescript code across node projects

自作多情 提交于 2021-02-08 15:19:18

问题


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 !


回答1:


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.




回答2:


I would like to know what other fellow typescript developers do in the community to share their code when using npm

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

I would simply reorganize as

  • webapps
    • package.json
    • projectA
      • SomeClass.ts
    • projectB
      • SomeClass.ts
      • Package.json
    • common
      • LoggingClass.ts


来源:https://stackoverflow.com/questions/46882060/share-common-typescript-code-across-node-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!