samanime
samanime

Reputation: 26615

Does NPM support installing dependencies via a Git URL to a sub-folder within the repo?

I checked the documentation and it didn't look like there was a way, but I couldn't explicitly tell.

I know it does and I know how to setup a Git repo itself to serve as a dependency, something like this:

But what I would like to do is basically use a monorepo pattern (like Babel), so I'd have a folder structure similar to this:

And then be able to install them all from the same repo just referring to packageA, packageB and packageC.

I know an alternative would be to have them all be part of one module, and then import them like package/packageA, but some of them aren't super related conceptually so that is less than ideal. The only reason they are in the same repo because I don't want NPM module repos clustering up our repository.

I know this is normally done with a private NPM repository, but we currently don't have access to one, so hoping there is an alternative.

Any ideas?

Upvotes: 7

Views: 3727

Answers (2)

HankScorpio
HankScorpio

Reputation: 3651

Figured I'd add my solution here in case someone else could benefit from it. This solution assumes you have full control over the repository and it's specifically made for the purpose of hosting your own NPM packages.

NPM supports targeting specific branches of a repository. Create a new branch for each package you wish to host.

  • MyRepo
    • develop (empty branch. Maybe put your Readme and .gitignore file here for other repo contributors)
      • packageA (branch containing ONLY the files for Package A)
        • package.json etc. (Files must be at the git root)
      • packageB
        • package.json etc.
      • packageC
        • package.json etc.

In your package.json file, target your branched packages like this:

    "packageA": "github:MyUserName/MyRepo.git#packageA",
    "packageB": "github:MyUserName/MyRepo.git#packageB",
    "packageC": "github:MyUserName/MyRepo.git#packageC",

Upvotes: 0

real_ate
real_ate

Reputation: 11299

It looks like https://gitpkg.now.sh/ is potentially what you want

Looking at the link that it produces it seems that it is acting as a proxy for your package in some way, so I wouldn't use this in mission critical production code but it should be fine if you're trying to test that something works between repos

Upvotes: 2

Related Questions