7

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:

  • (other files)
  • package.json

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:

  • packages/
  • packageA/
    • package.json
  • packageB/
    • package.json
  • packageC/
    • package.json

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?

8
  • You cant' have multiples files with the same name. You'll be able to only one packages/package.json Commented Apr 29, 2020 at 19:28
  • Sorry, that was a goof in formatting. The package.json is nested under packageA, B, C. I'll fix it. Commented Apr 29, 2020 at 19:40
  • npm will install dependencies in node_modules. You can define sub-folders in node_modules but not install dependencies in different folders. Maybe you want to use git submodules instead Commented Apr 29, 2020 at 19:48
  • Not quite. I basically want multiple Node modules per Git repo (without a private repository). Using Git submodules would be multiple Git repos per Node module. Commented Apr 29, 2020 at 19:55
  • 1
    AFAIK it's still a "No". This feature was requested in issue #2974 a few years ago. The npm docs state you can target either; a #<commit-ish> (i.e. specific git commit), or a <semver> (i.e. specific git tag) - unfortunately targeting sub-folder(s) within a git repo is not mentioned. However, in issue #4725 for yarn the gitpkg package is mentioned that also works with npm - I haven't used it myself, maybe worth a try. Commented Apr 30, 2020 at 8:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.