A
A
Alexander Lysenko2017-08-04 12:51:41
Node.js
Alexander Lysenko, 2017-08-04 12:51:41

Can someone explain about the dependencies in node_modules?

Hello. Not very strong with node.js and npm.
First, please advise what can be read on npm?
About the question. For example, I installed dependencies, installed a package, for example, and now I want to change something in it, add my functionality, for example, I can’t do this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
Oleg Lustenko, 2017-08-04
@fakey0u

you can write patch over a module.
Each module at the time of require('some-module') gets into require.cache and then this module can be patched.
For example:

// first.js
const add = (a,b) => a +b;
module.exports.add = add

// patched.js
const MODULENAME = './first';
const moduleToPatch = require(MODULENAME); // здесь мы кешировали нужный нам модуль

require.cache[require.resolve(MODULETOPATCH)].exports.add = (a,b) => {
 console.log('PATCHED ! ! ! ! ')
 return a + b;
}

// second.js
require('./patched');
const { add } = require('./first');

console.log(add(10, 20)) 
// PATCHED ! ! ! !
// 30

Naturally, you first need to connect the module for the patch so that it makes the first require in itself

K
Konstantin Kitmanov, 2017-08-04
@k12th

You go to the author of the package and say: so and so, I need this kind of behavior, can I make a PR? If you agree on how it should look and work, then fork, make changes, send a pull request to the author.

E
emp1re, 2017-08-04
@emp1re

For example, I installed dependencies, installed a package, for example, and now I want to change something in it, add my functionality, for example, I can’t do this?

You create your own module / lib, connect all the necessary dependencies inside it, wrap the functions you need and then use your implementation in the application. Do not copy modules, it will be difficult in the support.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question