46

Let's say there is an npm package called abcd. Normally in package.json, we specified the dependencies as "abcd": "^1.0.0",

But this abcd does not work as expected, so I forked (and modified) it inside https://github.com/mygithubid/abcd

Then I run npm install git+https://git@github.com/mygithubid/abcd.git and in package.json, the definition is changed to "abcd": "git+https://github.com/mygithubid/abcd.git",

After restarting the project that using this abcd, now it throws error Module not found: Can't resolve 'abcd' ... even though I saw the abcd folder is added inside node_modules

Could you advise the mistake I made in above? Thanks!

iwan
  • 7,269
  • 18
  • 48
  • 66
  • Possible duplicate of [How to install an npm package from GitHub directly?](https://stackoverflow.com/questions/17509669/how-to-install-an-npm-package-from-github-directly) – str Nov 05 '17 at 14:00
  • 1
    npm install github:mygithubid/abcd . check out – Himanshu sharma Nov 05 '17 at 14:01
  • Actually the new `abcd` is added in `node_modules`, but the application still complaining of `Module not found: Can't resolve 'abcd'` – iwan Nov 05 '17 at 15:29
  • if you don't mind forking and downloading it locally: https://stackoverflow.com/questions/14381898/local-dependency-in-package-json – AIon Nov 05 '17 at 15:47
  • Thanks, Alon, it would be little problematic if I need to download locally... what I was trying is to fork in Github.com and modified in Github.com too. Is that not the common approach? – iwan Nov 06 '17 at 03:17
  • I am having the exact same issue. Did you ever figure out a solution? – kat May 17 '18 at 15:26

2 Answers2

32

One fairly clean option is to use patch-package: https://www.npmjs.com/package/patch-package

If the people using your project might use either npm or yarn, then remeber to make the patch available for both. More info under patch-package --use-yarn.

Patch or fork? See https://www.npmjs.com/package/patch-package#benefits-of-patching-over-forking

Simon B.
  • 2,530
  • 24
  • 30
7

Be sure that the github repository contains compiled files, at most cases you need build the package for npm first, the compiled files push only to NPM. To find out how to build the package check the package.json file

  • 1
    can you provide more detailed steps to "find out how to build the packages" or refer to a guide at least? – lacostenycoder Mar 30 '21 at 14:08
  • @lacostenycoder it depends on the plugin itself. It is impossible to give an universal answer. Anyway, if you don't know where to start, you can open the `package.json` file and check the `scripts` property, it usually contains a `build` script. – Sebj Jul 01 '21 at 14:43