Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - Import functions from other file and use as if it had them before?

Let's say I have a file called server.js:

//server.js
    var net = require('net');
    var server = net.createServer(function (socket){
    socket.on('data',function(data){
        console.log("Received: "+data);
    });
});

And another file called functions.js:

//functions.js
module.exports = {
  test: function () {
    // do something
  },
  other: function () {
    // do something
  }
};

I know I can do something like this:

var functions = require('./functions');
functions.test();

But I really wanted to be able to use the function directly, like this:

require('./functions');
test();

Any suggestions?

like image 697
MagisterMundus Avatar asked Dec 25 '22 22:12

MagisterMundus


2 Answers

While you could load them into the global variable, another possible, and perhaps more sane* way to do that would be to assign the method to the variable test.

var test = require('./functions').test;
var functions = require('./functions');
var test2 = functions.test;

* = I say sane, because touching the global scope can have some unexpected results, cuts down on general readability, (Hey, where did this test variable come from? I didn't see it defined anywhere in the code.), and worst case scenario, you might run into name clashes, or even overwrite something you need in the global scope. Not to say that loading some things into the global scope isn't a good idea, or even downright useful, but often times, it's best to keep things scoped locally for readability.

An article on touching the global scope in the browser.

like image 78
Brandon Anzaldi Avatar answered Dec 28 '22 09:12

Brandon Anzaldi


Well, you could put the functions into the global variable so that they become accessible from anywhere in your code, but this is generally discouraged in favour of the preferred usage, as you already outlined in your code example.

If you insist on putting the variables in global scope, you can do the following:

// function.js
global.test = function test () {/* ... */}
// ...

// Somewhere else...
require('./functions')
test()

To at least partially improve on this, you could implement it so that the function.js file really only exports the functions as you currently do, and the individual functions get assigned to the global scope only by the requiring module. This way, you could at least properly test the functions.

Update: You did not say whether or not you would like the function to be available like that throughout the whole application or only in the current scope (where you require them) - if you only need them to be in current scope, it is then preferred to steer clear of the global variable and instead assign the functions to variables:

var functions = require('./functions')
  , test = functions.test
  , other = functions.other

// Use as necessary
test()
like image 42
Robert Rossmann Avatar answered Dec 28 '22 11:12

Robert Rossmann