173
votes

I'm using the node_swiz module, which in turn uses the validator module.

I want to make changes to the validator module, but I used npm install to install the modules/dependencies.

Can I just make changes to the validator module inside of node_modules, or will that node_modules dependencies be re-created and the latest version gotten when I publish to heroku or next time I run npm install?

The structure looks like this:

myNodeApplication
  - node_modules
     - swiz
         - node_modules
            - validator [this is the library I want to edit]

Thanks for the help!

4

4 Answers

291
votes

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>

90
votes

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" 
16
votes

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:

  1. Edit your package.json file to remove the dependency you want to edit.
  2. 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
  3. cd into the dependency directory and type npm link
  4. cd into the root of your project directory and type npm 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.

6
votes

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