Jovani
Jovani

Reputation: 101

`npm install` from sub directory of a Git repository

Greeting, I have created an angular library and I have built & pushed it into private repo in gitlab, now I need to consume it inside another project

so I have tried to import it using the following line in package.json

"my-core-lib": "git+ssh://[email protected]:<username>/my-core-lib.git"

But that was downloaded the whole repo and put it inside node_modules including "dist" folder

I need to import the content of "dist" folder and not the whole repo something like:

"my-core-lib": "git+ssh://[email protected]:<username>/my-core-lib.git/dist"

Thanks in advance

Upvotes: 10

Views: 6667

Answers (2)

Shayan Toqraee
Shayan Toqraee

Reputation: 867

If the package source is hosted on GitHub, you can use GitPkg like this:

# using npm:
npm install https://gitpkg.vercel.app/<user>/<project>/<subdir>?<commit-ish>
# using yarn:
yarn add https://gitpkg.vercel.app/<user>/<project>/<subdir>?<commit-ish>

There is a nice wizard-like form at their site that helps build the URL, and the command to install it.

Upvotes: 5

Andrey Tserkus
Andrey Tserkus

Reputation: 3634

If you build a custom library, and the entry point is not in the root of its Git repository, then you merely need to perform some post-install processing - to move your source to the proper place.

Optionally, you can also apply file filter, so that npm copies from the repository just the files/directories you need in the module.

Edit package.json and add two lines: files and scripts -> postinstall, so that the file looks like this:

{
  "name": "my-custom-angular",
  "version": "1.0.0",
  "main": "index.js",
  "files": ["dist"],
  "scripts": {
    "postinstall": "mv ./dist/* ./ && rmdir ./dist"
  }
}

GL!

Upvotes: 1

Related Questions