How the hell do I use my forked NPM package?

dannyaziz97

Danny Aziz

Posted on August 19, 2019

How the hell do I use my forked NPM package?

You can install your fork by doing npm install github:[GITHUB_USERNAME]/[GITHUB_REPO]

But the package won't work out of the box. Why?

Most of the time the /dist of the package is placed into the .gitignore. So you need to build a packaged version of the package so your project can use it.

To do this there are 2 methods. Only one worked for me.

Method 1 (The one that didn't work for me 🤷‍♀️)

Inside your package.json you add a postinstall that goes into your directly and runs npm install and npm run build

  "scripts": {
    "postinstall": "cd node_modules/[PACKAGE_NAME] && npm install && npm run build"
  },

Now just run npm install and your package should be updated to your fork.

What if it doesn't work?

For a package I was testing it out on, npm install worked perfectly but the build process would never work if the package was already inside node_modules...

Method 2 (Branch method)

This method requires you to make a branch on your fork that will only be used for installing (until the master of your fork gets merged, hopefully)

  1. Create a new branch:
    git checkout -b useLocally

  2. Remove /dist from the .gitignore

  3. Add the build command to precommit:

 "precommit": [
     "build"
   ],

Push Branch

git add *
git commit -m "COMMIT_MESSAGE_HERE"
git push origin useLocally

Now install the branch into your project
Just append #[BRANCH_NAME] to the URL of the repo when installing
npm install github:[GITHUB_USERNAME]/[GITHUB_REPO]#[BRANCH_NAME]

Now the /dist will be installed without having to make any changes to the package.json on master!

💖 💪 🙅 🚩
dannyaziz97
Danny Aziz

Posted on August 19, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related