14

I have a very basic function that looks like this, it is in a file called functions.js

function echo(input){
    process.stdout.write(echo);
}

When I add the file and call echo() like so:

Main file:

require("functions.js");
require("another_file.js");

another_file.js

echo("hello!");

It is giving me the following error:

ReferenceError: echo is not defined

Is there a way for me to make a function that is global like that without havingin to use exports?

2
  • 1
    try echo = function echo(input){ ... }
    – Plato
    Apr 9, 2015 at 16:50
  • 1
    @Plato: better be explicit by doing global.echo = …. Assigning to uninitialised variables is an error (in strict mode)
    – Bergi
    Apr 9, 2015 at 17:16

1 Answer 1

30

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");
5
  • You don't need that IEFE, node already has a global global variable. It might even not work at all because this in the module scope is not the global object iirc.
    – Bergi
    Apr 9, 2015 at 17:17
  • 1
    Oops! You're right - this is not global nor module. Editing my answer now
    – redbmk
    Apr 9, 2015 at 19:16
  • Should have done some testing before. Also found out that "use strict"; doesn't cause it to throw an error if you just do echo = ... but also doesn't set it to the global (I'm more familiar with the browser environment than node). Edited answer to reflect that
    – redbmk
    Apr 9, 2015 at 19:28
  • Wait, echo = … doesn't throw in strict mode? Then there must be a declaration somewhere. odd. edit: I've tested this now in node, and it does throw indeed.
    – Bergi
    Apr 9, 2015 at 19:55
  • @bergi, ah, looks like i was saying function echo() { }; echo = ......changing it to just echo = function echo() { } does cause it to throw in strict mode
    – redbmk
    Apr 9, 2015 at 22:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.