Reputation: 395
I need to download a node package from a private monorepo in GitHub, similar to this:
monorepoProject
--- subProjectA
--- subProjectB
And both subProjectA and subProjectB are typescript projects, that looks like the diagram below:
subProjectB:
--- dist
--- src
--- index.ts
--- myCode.ts
--- package.json
--- README.md
--- tsconfig.json
This is the project`s link:
https://github.com/company/monorepoProject
To clone via SSH:
git clone [email protected]:company/monorepoProject.git
I tried to use gitpkg like this:
yarn add 'https://gitpkg.now.sh/company/monorepoProject/subProjectB?commitHash123123'
but I had no success. What can I do to download a node package from a subdirectory in Github?
Upvotes: 4
Views: 8282
Reputation: 2420
Let's use my monorepo, https://github.com/codsen/codsen — a monorepo of bunch of npm packages — as an example.
In short, it depends what you mean by "need to download". Also, do you need script/UMD/IIFE or a ESM/CJS build.
If you want to contribute, clone whole monorepo on GitHub, work on the particular sub-package (cd
into packages/*
and use that package's npm scripts such as test
etc). For example, you can see few people forked my monorepo. They would play around and sometimes raise a PR from their fork.
If you want to consume one of the monorepo sub-programs, theoretically, maintainers should publish it to npm and you'd consume it from npm, via normal npm i
flow. So, you don't have to pluck the package out of monorepo at the first place.
If it's not possible to consume from npm
, and you have a working monorepo, theoretically, you can symlink root of each package manually into your project's node_modules
. That's dirty but it is possible. Snippet is like this: ln -s /Users/roy/packages/detergent /Users/roy/packages/object-all-values-equal-to/node_modules/detergent
(here I symlink detergent
into object-all-values-equal-to
node_modules, so detergent
could be consumed as a dependency).
Now, it's unlikely, but if somehow you really need to pluck the program out of the monorepo, let's say it hasn't been published to npm, maybe it's for internal use. In that case, check the licence, if it's permissive open source licence, you can git-clone the whole monorepo, copy-paste the files you need out of it into your project — just properly attribute the creator (typically, copy-paste their MIT licence under your MIT licence, within the same file and possibly mention what files are theirs) — make sure the licence is Open Source, for example, MIT (check in package.json
). The challenge will be patching up missing imports but it can be done.
For example, I have maintenance scripts - if you'd want to take them and use them in your monorepo, you'd have to copy-paste relevant files and attribute by storing my MIT licence along your licence.
If you need a monorepo-based package to drive your web project, normally you'd consume that package from npm and use bundlers such as webpack. In tiny projects, you might find it faster to grab the UMD/IIFE script, either directly from CDN's or save it as a local file. For example, I build, commit and npm-publish IIFE builds of non-CLI's, they're available from JSDelivr and others. For example, <script src="https://cdn.jsdelivr.net/npm/detergent/dist/detergent.umd.js"></script>
.
Also, when you say "private monorepo" I'd first understand it as closed-source project, the "private packages" as npm uses the term. It doesn't affect the whole consumption matter, only privacy and legal aspects. But maybe you meant "personal monorepo" or "somebody's monorepo"?
If it's a really private monorepo, a truly private, closed-source corporate package, you'd need to set up credentials, see npm page.
Also, when you mentioned GitPkg. I checked it, it's strange. Let's take my monorepo as an example, let's say you wanted to grab detergent
package from git. Well, bad news, I don't commit the build artifacts (dist/
), I commit only source files, so something along the line of npm install 'https://gitpkg.now.sh/codsen/codsen/packages/detergent?main'
is not possible because detergent-the-program comes from git-ignored (but not npm-ignored) dist/
folder. But jsDelivr does pick up packages previously published to npm, for example, see https://www.jsdelivr.com/package/npm/detergent?path=dist — notice the UMD and ESM builds, but they come from npm, that's not consuming from GitHub.
Upvotes: 6