简体   繁体   中英

Operation module.exports and require

There's one thing I don't really understand with the module.exports and the require() with NodeJS. As I can't explain it correctly, I can't find the information I am looking for so I'm here to ask for a little help finding answers.

I can export functions like:

example1.js

module.exports = {
    hello: function() {
        return 'Hello world';
    });
}

OR

example2.js

module.exports = function(data) {
    return data;
}

And I can use it with something like:

console.log(require('./example1.js').hello());

OR

console.log(require('./example2.js')('hello world'));

Starting from there, if I call NPM modules in my server.js like : var bcrypt = require('bcrypt'); and I want to use it in an another module, it's better to pass it to the function like :

example3.js

module.exports = function(bcrypt) {
    return  bcrypt.hashSync('hello', 5);
}

And after (to log the hash) :

console.log(require(./example3.js)(bcrypt));

Or to declare it directly in the module ?

example3bis.js

var bcrypt = require('bcrypt');
module.exports = function() {
    return  bcrypt.hashSync('hello', 5);
}

Another question, if I declare bcrypt in the server.js and in a specific module like example3bis.js , does that mean I'm duplicating the code?

Last, are there good practices to follow with the modules?

I don't know if I explain things clearly, but I try to understand things around the modules (npm or exports) and how the code is duplicated or not if I call it multiple times.

Thanks in advance for your answers !

Okay, here are the answers

  1. require the module in the file that is using the module, don't pass it around.

  2. require just maintains a cache of modules that was loaded, so require ing the module multiple times does not have any performance penalty, and there is no code duplication as well

So in general, no need to worry at all about code duplication or performance issues, these are already considered in the module system, just require the modules, in the files that need it instead of passing around as parameter, which results in cleaner code

You can do it either way, but I would suggest just require()ing it again in other modules where you need it. This way you are not caching and passing along the module yourself which would consume slightly more resources.

Modules are cached, so the first time you require() a module, it is in memory and stays there until removed. So there is no code duplication. So just require the module you need in every other module and you will get the same stored/cached copy of its exports.

As Topian said you can use both way,

But, you should use require() when require Like:
var bcrypt = require('bcrypt'); module.exports = function() { return bcrypt.hashSync('hello', 5); }

it's look better than passing around parentheses

Logically both way are right as Jiby Jose said no issues for duplication code and performance of app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM