Making changes is easy. The important thing is to preserve the changes. A change to node_modules
occurs any time we install or remove a package.
To preserve the changes, we can add the file to version control by running:
git add -f <path to the file in node_modules>
This way, the original version can be restored from Git.
The next step is automating this. To do so, we can define a wrapper shell function for the npm
command. For example, in Bash:
npm ()
{
case "$1" in
install|remove) env npm "$@" && git checkout "$(env npm root)" ;;
*) env npm "$@" ;;
esac
}
npm root
points to the node_modules
path in the current npm
project. This way, whenever we successfully run install
or remove
on npm
, it will automatically be followed by checking out node_modules
in the current repository, hence restoring the custom files in node_modules
.