Skip to content

Instantly share code, notes, and snippets.

@dmfrancisco
Last active May 26, 2020 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmfrancisco/7aa5138ae1dbc5748c2994956907acd0 to your computer and use it in GitHub Desktop.
Save dmfrancisco/7aa5138ae1dbc5748c2994956907acd0 to your computer and use it in GitHub Desktop.
Forking a npm package

Forking a npm package

If you need to make some change to a package you use, you don't need to publish your changes to npm. The npm and Yarn clients let you install dependencies directly from GitHub. So if you had to fork, let's say the history package, you can install your fork with one of the following:

npm install github:your_github_username/history
# or
yarn add github:your_github_username/history

And every time you need to update it just run upgrade with the package name instead of the short repo notation:

npm upgrade history
# or
yarn upgrade history

The problem is that for many packages you need to build the source before using it. These build files are included in the published package you download from the npm registry, but aren't available in the GitHub repository. This happens because they are listed in the project's .gitignore, since they aren't source and don't belong in source control.

One solution is to just change the .gitignore file and publish those files. An alternative option is to use a prepare script instead.

Going back to our example, this is how history's package.json looks like:

{
  "name": "history",
  "version": "4.7.2",
  "description": "Manage session history with JavaScript",
  // ...
  "scripts": {
    "start": "webpack-dev-server -d --content-base ./ --history-api-fallback --inline modules/index.js",
    "build": "node ./tools/build.js",
    "prepublishOnly": "node ./tools/build.js",
    "clean": "git clean -fdX .",
    "lint": "eslint modules",
    "test": "karma start --single-run"
  },
  // ...
}

Simply replace prepublishOnly with prepare and you should be good to go. In recent versions of npm and Yarn, prepare is executed after the package is installed from GitHub.

@Hanzofm
Copy link

Hanzofm commented Feb 12, 2020

Hi, I followed your tutorial and it not works for me.

I have forked the repo
https://github.com/hsuanxyz/ion2-calendar
into my account
https://github.com/Hanzofm/ion2-calendar

On v2 branch I have done changes on https://github.com/Hanzofm/ion2-calendar/blob/v2/src/components/calendar.component.ts file commenting the content of a method via github online editor.

Then on package.json I have added this line to the scripts section

"prepare": "npm run build"

Then I have downloaded the forked repo via

npm install git+https://github.com/Hanzofm/ion2-calendar#v2

It installs correctly and but the commented method stills uncommented.

What I would be doing wrong?

@dmfrancisco
Copy link
Author

Hey @Hanzofm, I ran into this issue too recently. In my case it was because there was a .gitignore file to ignore those build files but no .npmignore file. In this case what happens is that NPM ignores everything listed in the .gitignore. So to solve the issue you simply have to create an empty .npmignore file. I found the solution here which links to the official docs here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment