1

Is it possible to to have this return an output ?

hello.js

var y = 1;

console.log(x);
console.log(y);

main.js

var x = 42;

var magic = somehowInclude('hello.js');

And when you run main.js with node it prints 42 and 1

Is it to possible to do this without require and exports ?

Kartik
  • 9,463
  • 9
  • 48
  • 52
  • some solutions are posted here... http://stackoverflow.com/questions/950087/include-javascript-file-inside-javascript-file I think you are looking for that jQuery solution mentioned in accepted answer of that question – Gatekeeper Apr 11 '12 at 07:10
  • @Gatekeeper but this is not client side at all, all this is server side running in node and a web server sadly isnt part of the the setup. – Kartik Apr 11 '12 at 07:18

1 Answers1

1

Use Node.js modules.

hello.js

module.exports.magic = function (x) {
  var y = 1;

  console.log(x);
  console.log(y);
};

main.js

var x = 42;

var hello = require('./hello.js');
hello.magic(42);

Read the detailed description of the module loading system at in the Node.js documentation.

Alan Gutierrez
  • 15,339
  • 1
  • 18
  • 17