Modifying an npm-installed dependency: Is it possible?

Hey everyone, I’m working on a project that uses the node_swiz package. This package depends on another module called validator. I need to make some tweaks to the validator module, but I’m not sure if it’s safe to do so.

My project structure looks like this:

myApp
|- node_modules
   |- awesome_package
      |- node_modules
         |- cool_validator (I want to change this)

I installed everything using npm. If I modify the cool_validator files directly in the node_modules folder, will those changes stick around? Or will npm overwrite them when I deploy my app or run npm install again?

I’m worried about losing my edits. Any advice on the best way to handle this situation would be super helpful. Thanks in advance!

I’ve been in a similar situation before, and I can tell you it’s not advisable to directly modify files in the node_modules folder. Those changes won’t persist and will be overwritten when you run npm install or update the package.

Instead, consider forking the cool_validator repository and making your changes there. Then, you can point your package.json to your forked version using the GitHub URL. Something like:

“cool_validator”: “github:yourusername/cool_validator#branch-name”

This way, your changes will be preserved, and you can easily update or revert if needed. It’s a bit more work upfront, but it’ll save you headaches down the line.

Another option is to use npm’s patch-package. It allows you to make and persist changes to installed packages. You make your edits, run patch-package, and it creates a patches folder with your modifications.

Both approaches have their pros and cons, so choose what fits your workflow best. Good luck with your project!

Modifying node_modules directly is a bad idea, mate. those changes wont stick around. instead, try npm-patch or create a local fork of cool_validator. then update ur package.json to use ur forked version. its a bit more work but itll save u headaches later on. good luck with ur project!

I’ve encountered this issue before, and it’s definitely not recommended to modify files directly in node_modules. Those changes won’t persist and will be overwritten during npm installations or updates.

A more robust approach would be to create a wrapper module for cool_validator. This way, you can extend or override its functionality without touching the original package. You’d create a new module that imports cool_validator, applies your modifications, and then exports the modified version.

In your main application, you’d then use this wrapper instead of the original cool_validator. This method allows you to maintain your changes separately, making it easier to update the original package when needed and keeping your modifications intact across installations and deployments.

Remember to document your changes well for future maintenance. This approach might require a bit more initial setup, but it’s a cleaner and more maintainable solution in the long run.