2

I'm running JavaScript from a node.js build I implemented into Sublime Text 3.

From ReferenceError: "alert" is not defined, I understand that since the script isn't run from a browser, alert boxes are disabled.

Are there any alternatives to this? I don't mind losing the alert popup, but I want Sublime Text to print out the alert and not catch an error on it.

Community
  • 1
  • 1
unbindall
  • 514
  • 1
  • 13
  • 29
  • 2
    node.js is a server side framework. How would you show an alert box in the server? – thefourtheye Mar 12 '15 at 00:09
  • 2
    ... You can name a function whatever you want. – Dave Newton Mar 12 '15 at 00:11
  • how about `console.error`? More to the point though, you seem a little confused about what you want. What is your objective here? To suggest a problem to a client of your system, or to log an error for your use? – bcr Mar 12 '15 at 00:14
  • 2
    An interesting part of ECMAScript is that it has no input or output mechanism, it must be provided by the host environment. – RobG Mar 12 '15 at 00:15
  • 2
    @DominatorX ... That's quite a bit different. Perhaps you want something ncurses-like? I mean, there's no "alert box" in a console window. Or to interact with Growl or whatever notification mechanism your OS uses? – Dave Newton Mar 12 '15 at 00:16
  • if you're just making an application to interact with on the command line, I really don't think you want the interaction you're describing. If you're trying to make a terminal-based but more complicated interaction than tty, you really need to look into an existing framework to support this, as dave says – bcr Mar 12 '15 at 00:20
  • 1
    @DominatorX But Sublime is Python... You want it to scan the *source* of your JS, and when that *source* is executed, do something?! You *might* be able to do something by having Sublime look at the *output* of your program, but that again is different than what you think you want. I don't really understand; if you're trying to debug, use a debugger. If you're trying to make game elements, then use something that's console.based, e.g., look at Nethack. – Dave Newton Mar 12 '15 at 00:42
  • 1
    I think you'd be better off to forget emulating 'alert' for a console game and instead get used to dumping errors/debug statements to a log and using a real debugger. – bcr Mar 12 '15 at 01:08

1 Answers1

6

Upon starting your script ask for the typeof alert and define it if is undefined.

if((typeof alert) === 'undefined') {
    global.alert = function(message) {
        console.log(message);
    }
}

This should do the trick.

Gus Ortiz
  • 652
  • 3
  • 9