Publish your npm package to a Git branch.
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
For testing published packages without publishing to npm.
Making a prerelease to npm using npm publish
has the following drawbacks:
-
Needs a unique version for every release.
-
Hard to remove due to npm's strict unpublish policy.
-
Cumbersome to verify the contents of the published package.
-
Production environment—risky if you make a mistake (eg. accidentally publish as stable).
Using npm link
has the drawback of not running npm life cycle scripts and including non-publishable assets.
In contrast, git-publish
has the following benefits:
-
Unversioned Instead of versions, branch names are used. Branches can be updated to reflect latest change.
-
Deletable Simply delete the branch when you're done with it.
-
Browsable Use GitHub to verify the contents of the branch. You can even share a link for others to see.
-
Dev environment Low risk of mistakes.
-
Simulates
npm publish
Runs npm life cycle scripts and only includes publishable assets.
Publish your npm package to a branch on the Git repository:
npx git-publish
⚠️ Warning: This command will force-push to the remote branchnpm/<current branch>
. Make sure there are no unsaved changes there.
Keep the command handy by installing it globally:
npm install -g git-publish
When globally installed, you can use it without npx
:
git-publish
Flag | Description |
---|---|
-b, --branch <branch name> |
The branch to publish the package to. Defaults to prefixing "npm/" to the current branch or tag name. |
-r, --remote <remote> |
The remote to push to. (default: origin ) |
-d, --dry |
Dry run mode. Will not commit or push to the remote. |
-h, --help |
Show help |
--version |
Show version |
-
When you want to test a new package that isn't ready to be published on npm.
-
When you're contributing to an open source project so you don't have publish access, but want to test the changes in a production-like environment.
-
When you want to test in a remote environment so you can't use
npm link
. -
When you want to avoid using
npm link
because of symlink complexities.
Call the build command it in the prepack
script.
- Run npm hooks
prepare
&prepack
- Create a temporary branch by prefixing the current branch with the
npm/
namespace - Detect and commit the npm publish files
- Force push the branch to remote
- Delete local branch from Step 2
- Print the installation command for the branch
-
There can be missing distribution files (eg. files outside of
dist
). git-publish uses npm-packlist —the same librarynpm publish
uses—to detect publish files declared viapackage.json#files
and.npmignore
. -
Irrelevant files are committed (eg. source files). This can slow down installation or even interfere with the library behavior. For example, if your project has development configuration files, they can accidentally be read by the dependent tooling.
-
npm hooks are not executed. git-publish simulates package packing and runs hooks
prepare
andprepack
.
Yes, if using a Git client authorized to access the private repository.
If it must be publicly accessible, you can set the --remote <remote>
flag to push the publish assets to a public repository. It's recommended to compile and minify the code if doing this with private code.
You want to test a branch on a private repository Repo A, but GitHub Actions on the consuming project Repo B doesn't have access to the private repository so npm install
fails.
To work around this, you can publish the branch to Repo B to install it from there:
$ npx git-publish --remote git@github.com:repo-b.git --branch test-pkg
✔ Successfully published branch! Install with command:
→ npm i 'repo-b#test-pkg'