Inside functions.js
you'll have access to node's global
variable, which is like the window
variable in a browser. As Plato suggested in a comment, you can add this to the global by simply doing echo = function echo(input){ ... }
. However, this will throw an error if you're using strict mode, which is intended to catch common mistakes (like accidentally creating global variables).
One way to safely add echo
as a global is to add it to the global global
variable.
"use strict";
global.echo = function echo(input) {
process.stdout.write(input);
}
I think generally using exports
is better practice, because otherwise once functions.js
is included (from any other file) you'll have access to echo
in every file and it can be hard to track down where it's coming from.
To do so you would need to have your functions.js
look more like:
"use strict";
module.exports.echo = function echo(input) {
process.stdout.write(input);
}
Then in your main script do something like:
"use strict";
var functions = require("./functions.js");
functions.echo("Hello, World");
echo = function echo(input){ ... }
global.echo = …
. Assigning to uninitialised variables is an error (in strict mode)