5

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 !

Stacknerd
  • 439
  • 2
  • 8
  • 21

2 Answers2

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.

Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • Do you have an example of that ? I tried to dodge the node module because of the inconvenience to develop inside the node_modules directory. If we can do that differently, then that might become an option. Also, it is possible to create private npm modules ? – Stacknerd Oct 23 '17 at 14:30
  • `npm module -- which is de facto a standard in the JavaScript/TypeScript community` so let's say I have shared `StudentLib` and I want to edit one line there. Do I need to republish the package and then update in each project?? This is the worst solution so far, if this is the `standard in the JavaScript/TypeScript community` that says a lot – Toolkit Jul 03 '18 at 09:53
  • *You don't have to store it specifically in npmjs.org (you can have an own npm repository).* How does this work if you're using a CI system, like Netlify, where you push to git and it auto-deploys? I'd be happy to use `npm link` if this were somehow portable to somewhere other than my dev machine without having to publish to NPM. – devuxer May 11 '20 at 03:58
-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
basarat
  • 261,912
  • 58
  • 460
  • 511
  • If I understand well, you added a package.json file to the root. How does that work at compile time ? We have some node_modules in a higher folder, and some other node_modules in a lower folder – Stacknerd Oct 23 '17 at 14:30