Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override file or function inside node_modules

I need to replace a file from dependency, located in "node_modules". It is not a good practice to amend node_modules directly, as it will be overwritten with next "npm install". How can I replace such file without touching "node_modules"? For example we can do it with typings. But what about other files?

In my case I want to replace "foo.component.less" and "foo.component.metadata.json" files. Just creating new files in host doesn't help as it would overwrite access to css, but not metadata.

like image 373
Alex Vovchuk Avatar asked Apr 15 '19 17:04

Alex Vovchuk


People also ask

Can I make changes in node_modules?

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.

How do I remove unnecessary files from node modules?

You can use npm-prune to remove extraneous packages. Extraneous packages are packages that are not listed on the parent package's dependencies list. If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies.

What is .bin in node_modules?

The directory node_modules/.bin is where the binaries of the modules used by your project are stored, normally using symbolic links to the respective binaries in the corresponding module's directory.


1 Answers

  1. Optimal, but long solution: to create copy/fork of the package's source code:

    • copy/fork of the package's source code;
    • make updates;
    • import to project as dependency instead of original one;
    • update readme.md file not to miss in future upgrades.
  2. If we don't require to update files and structure entirely, it can be enough to place overriding files to the root of the project(as for Angular). For example such css file would override the same one in node_modules:

    • copy required file to project root;
    • update that file. Now npm will look it first.
like image 135
Alex Vovchuk Avatar answered Sep 19 '22 01:09

Alex Vovchuk