What is the proper way to "patch" a node_modules module?

Swiffy

Say I have a Node.js app, lets call it patched-app. This app is a simple app that uses prompt-sync to ask for user input and then does something with it.

So we create a folder patched-app and initialize it with npm init. We then run npm i prompt-sync and create a new file called index.js where my code would be. To make things a little bit more interesting, I will also create a git repository here and this is the result:

+---patched-app
|   |   .gitattributes
|   |   .gitattributes
|   |   .gitignore
|   |   index.js
|   |   package-lock.json
|   |   package.json
|   |   
|   +---node_modules
|   |   |   .package-lock.json
|   |   |   
|   |   +---ansi-regex
|   |   |       index.js
|   |   |       license
|   |   |       package.json
|   |   |       readme.md
|   |   |       
|   |   +---prompt-sync
|   |   |       index.js
|   |   |       LICENSE
|   |   |       package.json
|   |   |       README.md
|   |   |       test.js
|   |   |       
|   |   \---strip-ansi
|   |           index.d.ts
|   |           index.js
|   |           license
|   |           package.json
|   |           readme.md
|   \---.git
|      <<git stuff>>

Now the issue here is that I don't like something with this prompt-sync package and so I want to edit node_modules/prompt-sync/index.js and change it to my liking. I don't necessarily want to create a whole new module such as prompt-sync-swiffy (or do I?), as my changes are small enough so I don't have to download any new packages for prompt-sync, so I'm good with the existing dependencies of ansi-regex and strip-ansi.

I could just open up node_modules/prompt-sync/index.js, edit it and be done - and that's what I have pretty much been doing thus far because I don't know any better. I have recognized at least three major issues with this though:

  • I cannot update prompt-sync anymore or I might lose the changes I made (losing !== just breaking my patched changes)
  • I cannot basically run any package commands, such as npm audit fix etc, as those might also overwrite my changes
  • This doesn't work at all, if I want to have my project in a git repository, because I will obviously add node_modules to .gitignore and if anyone were to pull my repository and run npm install, it would install prompt-sync without my changes

I also don't necessarily want to fork prompt-sync and / or create a new "official" NPM package. What I want is a method that allows me or anyone else to run npm install normally, while somehow keeping my patches to modules local e.g. have a module_patches folder which isn't ignored by the repository.

It is OK if a newer version of prompt-sync breaks my patch. That would be on me to update the patch or use a locked in version of prompt-sync.

So what are my options?

Andrei

A nice way to patch NPM packages is using patch-package. It will automatically create patch files of your changes and apply them in postinstall hook.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to patch Wine for a custom PPA?

What is the proper way to use the node.js postgresql module?

What is the proper way to document a CMake module?

What is the proper way to pass a module as an argument?

What is the proper way to Patch an object with controller-runtime?

Proper way to update node_modules in a shared git repository

Proper way to do a PATCH request

WHat would be a proper way to require modules

What's a proper way to export a global constant from a module?

What's the proper way to setup a "pure" typescript module

What is the proper way to exit the current process from a kernel module?

Bitbake: proper way to "patch" a function from a recipe?

Angular 5 proper way to customize node_module .js File

Proper way to write a module in fortran

Is this the proper way to use the timeit module?

What is the proper way to load Raku modules using rakudo-star?

What is the proper way import python modules for dev_appserver?

monkey patch node module

What is the proper way to use only variables that contain data in Node JS?

Node.js: What is the proper way of splitting code?

In Node.js 7 what is the proper way to suppress UnhandledPromiseRejectionWarning?

What is the proper way to make a destructor for this binary node class?

What's the proper way to require in Node.js?

What is the proper way to set up Node.js and MySQL?

What's the proper way to create a static sentinel node

What is the proper way to wait for connections?

What is the proper way to implement CupertinoActionSheet?

What is the proper way to use inotify?

What is the proper way to do this in Rust?