Node.js requiring a script but not running it

In Node.js when you do

var otherscript = require('otherscript');

it runs the script on demand

I am wondering if there is a way to “require” a script without running it so that you can run it later when you want.

Is there a good reason why not?

+4
source share
2 answers

If you can edit "othercript" (no one else uses this script), you can simply wrap all the code inside the function and add it to the export. Example:

otherscript:

module.exports = function(){
  //original code goes here
};

Then use as:

var otherscript = require('otherscript');
var obj = otherscript();
+3
source

, /. , , require() / .

Mongoose, ( "mongoose" ) mongoose, connect() . ( "mongoose" ) , .

+1

All Articles