I have discussed how to Use GitHub instead of NPM to share and install NPM modules. But that blog post had a huge shortcoming - it did not deal with built or transpiled artifacts. More and more NPM packages are now transpiled from TypeScript for example, and thus usually have two folders: lib
and dist
. The GitHub repository only has the lib
folder with the original source. The built dist
folder is the one published to the NPM registry.
If we just install an NPM package from GitHub we will not get the dist
folder - it is not on GitHub. We could try building it ourselves ... but that is tricky, since the project might be a pain to build.
Today I had just this problem - I needed to quickly patch @percy/cypress NPM module to get around Webpack bundling problem there. I have opened a pull request with my work around from my fork bahmutov/percy-cypress branch find-percy-58.
Meanwhile I wanted to use my patched version in bahmutov/calculator project. I could simply install my fork using github:
reference
1 | npm i -D github:bahmutov/percy-cypress |
But this installation installs ... wrong files
1 | $ ls -l node_modules/\@percy/cypress/ |
I need to install the built module. So here is how to do this myself.
First, add the files: []
property to the package.json. It is more flexible than just .npmignore
since when using files
you can whitelist or blacklist files and folders. In our case, we want to only distribute the dist
folder with the NPM package. Compare the files before and after.
before adding files
property
1 | $ npm pack --dry |
Lots of stray files - no user of this NPM package needs tslint.json
for example. After adding files: ["dist"]
we get just the necessary file list.
1 | $ npm pack --dry |
Super. Now, push the code (using a separate branch for example) to GitHub. Start a new release there.
When you make a GitHub release you can drop binary files there.
To prepare a binary file, run npm pack
command (without --dry
option). This will give you a tgz
file like percy-cypress-1.0.5.tgz
. Drop this file into the GitHub release. Here is the fix-webpack
I have made.
Right click on the percy-cypress-1.0.5.tgz
file to grab the download url, in this case it will be https://github.com/bahmutov/percy-cypress/releases/download/fix-webpack/percy-cypress-1.0.5.tgz
. You can npm install
that URL!
So I went to my example project bahmutov/calculator and ran
1 | $ npm i -D https://github.com/bahmutov/percy-cypress/releases/download/fix-webpack/percy-cypress-1.0.5.tgz |
The installed NPM package is beautiful; it just has what is necessary.
1 | $ ls -l node_modules/\@percy/cypress/ |
The package.json keeps the long original download url. Thus this installation works on CI server the same way.
Everything is peachy: I have my fork, and I can install it from other NPM projects.