loading...

How the hell do I use my forked NPM package?

dannyaziz97 profile image Danny Aziz ・1 min read

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!

Posted on by:

Discussion

markdown guide
 

I'm confused about number 3. 'Add the build command to precommit' Does that go into package.json or somewhere else? I googled 'precommit' and found a git hook called 'pre-commit'. Or is this using a separate library like this one? npmjs.com/package/precommit