Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit a node module installed via npm?

Tags:

node.js

npm

People also ask

Can I edit node modules?

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.

How do I update a specific npm module?

To update a specific package, we need to run the npm update command followed by the package name. Sometimes, you want to update a package to the specific version in such cases you need to use npm install command by specifying a version number after the package name.

How do I change node modules version?

If you want to switch to the different version of Node, just type n in the terminal and you should see the Node versions listed. Use arrow keys to choose the version and press enter.


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>


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" 

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.


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