Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugultopu/a08ff58c7eaf9d215cc0f20c4e8a9440 to your computer and use it in GitHub Desktop.
Save ugultopu/a08ff58c7eaf9d215cc0f20c4e8a9440 to your computer and use it in GitHub Desktop.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment