How can I pass a variable while using `require` in node.js?

前端 未结 4 1474
生来不讨喜
生来不讨喜 2020-12-13 13:54

In my app.js I have below 3 lines.

var database = require(\'./database.js\');
var client = database.client
var user = require(\'./user.js\')         


        
4条回答
  •  伪装坚强ぢ
    2020-12-13 14:07

    I think what you want to do is:

    var user = require('./user')(client)
    

    This enables you to have client as a parameter in each function in your module or as module scope variable like this:

    module.exports = function(client){
    
     ...
    
    }
    

提交回复
热议问题