232

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 Answers 4

356

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>

Sign up to request clarification or add additional context in comments.

17 Comments

Thanks! That's exactly what I wanted to know. Also, for others reading this... I wanted to include the module that's on my github in the package.json file, and there's info here about that: stackoverflow.com/a/8306715/1810875
before forking I am changing it in the local system, but the change does not seem to affect. Changing any javascript file which is node_modules/package_name/lib/file_name.js does affect?
Just a note, not sure if something has changed but personally I had to specify the Git URL as git://github.com/<user>/<package>.git"
@JohnDevor Instead of installing the customized module, use the "npm link" command to just create a sym-link to its folder: docs.npmjs.com/cli/link
Figured it out, I had to navigate to the folder in /node_modules and run npm install then all is well!
|
223

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" 

10 Comments

Very nice solution which worked for me without any issues.
this is really cool! FYI: I had to use "preinstall" instead of "postinstall" because my patch was to the postinstall script of the dependency. How would you implement that if you had other patches that needed to be applied in postinstall phase?
Incredible answer, I needed to make modifications to a dep before deploying to netlify. preinstall 'npx patch-package' was the magic ticket. The proper image is now shown on the website.
Awesome answer! Thanks. Note that if your "change" in the package is in its package.json (e.g. updating an outdated fixed version dependency), you need to do npx patch-package <package name> --exclude. By default, it doesn't pick up the changes in package.json of sub-package.
@KyleVassella yes! patch-package is ran in the post-install script, so every time you run npm install, it patches the package. It'll run during your build process.
|
28

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.

7 Comments

Cons: 1) You can never update the package version, even if it has security issues. 2) If you (or your future developers) ever want to do so, they have to spend time and manually diff the entire package to find out what you have changed. 3) github.com/facebook/create-react-app/issues/5372 So, TL;DR: it can make your future self or teammates very frustrated.
That's what I ended up doing, but just copy the whole thing to src and import from './dependency' without link. No matter what I messed with the codes inside 'node_modules' directly, I just couldn't see any change being reflected on npm start. So frustrating...
@Aidin Yep, that is why recommended the other answer, but I'm glad you wrote that out since I didn't think to. Thanks!
Thanks! That's the way to go if one just wants to make some small quick change in the dependency. Cloning a repo in order to do such thing is a misunderstanding and overkill
Actually you don't need to remove dependency from package.json. In my case it caused build issues later. I've just edited this package.json entry, and instead of package version you need to specify path to your local library (like "package_name" :"file:your_path/to_local_lib",).
|
11

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.