How to have npm install a typescript dependency from a GitHub url?

試著忘記壹切 提交于 2019-12-01 03:17:40

The docs for the prepack script suggest that it is run after a dependency is installed from a git repo. Try putting something like this in the package.json of the git dependency:

{
  "scripts": {
    "prepack": "call the build script"
  }
}

This should build the package after you npm install it, which sounds like what you want to do. I'm not sure if there are any other problems you are having beyond that.

I had the same problem. Saw a lot of articles about monorepos (links below), but not much about how to split a TypeScript project into separate repositories.

In short, you need to build JS files at one step or the other.

See https://github.com/stared/yarn-adding-pure-typescript-package-example for a working code example.

So, there are a few solutions. Let's say that the repository is someuser/package-to-import

Postinstall

Using yarn you can get the project directly from a GitHub repository:

yarn add someuser/package-to-import#master

(or the same with npm install someuser/package-to-import#master)

If you work on a fork of this repository, you can make your life easier by adding to package.json in package-to-import repo this part:

  "scripts": {
    ...,
    "postinstall": "tsc --outDir ./build"
  }

Now it just works with yarn add/upgrade with no extra scripts. Alternatively, you can do it semi-manually, i.e. create a script in your main repository package.json

  "scripts": {
    ...,
    "upgrade-package-to-import": "yarn upgrade package-to-import && cd node_modules/package-to-import && yarn build"
  }

Link

There is a different approach to clone the repository into a different folder, build the files, and link it.

git clone git@github.com:someuser/package-to-import.git
cd package-to-import
npm run build  # or any other instruction to build
npm link

If you use yarn, the two last lines would be:

yarn build
yarn link

Then, enter the folder of your project and write

npm link package-to-import

or in case of yarn

yarn link package-to-import

These are symlinks, so when you pull from the repo and build files, you will use the updated version.

See also:

Monorepos

An entirely different approach. With mixed advice for using git submodules:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!