How to edit a node module installed via npm?
I didn't want to publish a new module and I also didn't want npm install
to overwrite my changes. I found a solution to both of these issues, but it would probably be better to take @Sdedelbrock's advice. But if you want to do it, here's how:
- Edit your
package.json
file to remove the dependency you want to edit. - Go into your project's
/node_modules
and move the folder somewhere else in your repository that can be committed. So now/node_modules/dependency
is at/dependency
cd
into the dependency directory and typenpm link
cd
into the root of your project directory and typenpm link dependency
It is important that you do this outside of/node_modules
and/dependency
If everything worked, you should now have a symlink that was created in /node_modules/dependency
. Now you can run your project to see if it works.
Fork the Github repo and make the necessary changes then you can install the package like
npm install git+https://github.com/visionmedia/express.git
You can use patch-package to make and persist changes to node modules.
This can be done by first making changes to the package inside node_modules
and then running the following command, with <package name>
being the name of the package you just made changes to.
npx patch-package <package name>
patch-package will then create a patches
folder with a file inside, representing your changes. This file can then be commited to git, and patches can be restored later by running npx patch-package
(without any arguments).
Optional step:
Add the following in the script section of your package.json to automatically patch the dependency when you execute "npm install".
"postinstall": "npx patch-package"
You can edit the file directly, but this would be overwritten whenever npm updates, the best thing to do is go straight to the source.
If the changes affect functionality of the overall module, and may be useful to others, you may want to contribute to the original source on github and look for the change to be implemented.
If this is proprietary functionality that is needed, and would not help the development of the module, the best thing to do is fork it from github and make your changes. You can install items directly from github using NPM, and this method would let you integrate future changes in to your custom version from the original source.
To install directly from github, use the following command:
npm install https://github.com/<username>/<repository>/tarball/<branch>