3

what should i do ?? I work on node.js . I can not use alert("sometext");

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var dbo = db.db("project");

  dbo.collection("Register").findOne({Email:  req.body.email} , function(err, result) {
  if (result.Password == req.body.psw) {
      console.log("Correct go next page");
    }
    else{
      window.alert("sometext");
    });
    db.close();
  });
});

output = ReferenceError: window is not defined

3
  • 1
    There is no window with node. It runs on the server. Commented May 29, 2019 at 3:25
  • How can i print Alert message sir. Commented May 29, 2019 at 4:00
  • Can you tell why you need : window.alert("sometext")? And are you are writing an API using MongoDB with Nodejs? Commented May 29, 2019 at 5:41

5 Answers 5

7

Assuming you know, which OS you are running on, the most reliable way would be to use it's native shell (so far, most NPM packages I tried did not work out of the box). For windows Powershell, it can look like this:

const { spawnSync } = require('child_process');

const messag = "Hello world";

spawnSync("powershell.exe", [`
Add-Type -AssemblyName PresentationCore,PresentationFramework;
[System.Windows.MessageBox]::Show('${messag}');
`]);

You can also show prompts, like yes / not, etc. See more info here

Update dialog package seems to work out of the box, though does not look to have as much options as what is available with PowerShell.

Sign up to request clarification or add additional context in comments.

5 Comments

This gives me: Unable to find type [System.Windows.MessageBox]. At line:1 char:79 + ... tationCore,PresentationFramework; [System.Windows.MessageBox]::Show(' ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Windows.MessageBox:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound
@ClausConrad any chance you are on Linux or using PowershellCore?
I tried with PowerShell Core 7.2.5 and Windows PowerShell 5.1 and got the same error message in both.
@ClausConrad im guessing the problem lies with Powershell on that specific machine lacking something necessary, maybe DLLs for presentation framework? Can run that code in PS directly and check? Alternatively, can you use the approach of running a process from Node just to show a message, but try using some other shell / process?
That was already running directly in PS. Just tried on another machine (also running Windows 11, could that be the problem?) and got the same error. I've found another approach, no issue, just curious why this did not work.
4

NodeJS runs on the console of the given operating system, that said, window.alert is actually a browser API command, you cannot use it, as alternatives, you can:

  1. Use console.log that will output information on the STDOUT(Aka the standard output, aka will just print on the console).
  2. Use console.error, that will do exactly the same as above, but will pipe the content to STDERR, that can be usefull for logging or process output identification purposes.
  3. Wrap your Node application on a container like Electron, that, to be honest, is overkill.
  4. And finally, calling the native dialog API from your OS:

To call a native dialog, you need acess to the OS native libraries, you can do that using the Node FFI module that allows you to bind those libraries. Or, for the sake of simplicity, just use one of existing implementations of that function, like: node-native-dialog, mitsobox or dialog.

Disclaimer: I did not develop or can attest the quality of those modules, use at your own risk, if native dialogs are really required to your development, implement your own binding of the OS libraries.

3 Comments

This could be useful: github.com/bat-tomr/dialog-node
to clarify, Electron is recommended here because it helps interface between Node and OS?
@shea This is not a recommendation but rather a list of possible options. Yes, Electron is on this list because it allows calling native dialogs(file picker, alerts, etc) easily. This answer was written 5 years ago, I highly recommend searching for other modern options :)
4

Install cross-platform, isomorphic alert, for Node and browser (previously alert-node) npm i alert -g

Use this lib:

var alert = require('alert');
alert('Hello');

1 Comment

Just in case someone comes across this, that package is deprecated in favor of node-notifier, which is much better than I had time to build in alert.
0

I can't find where it is documented but I have been using global.alert() in my react-native code for debugging purposes. The app is being controlled by Appium so I can't use console.log.

Comments

0

You can add a title and message body to the message box too.

const { spawnSync } = require('child_process');

const title = "My Title";
const message = "Hello, world!";

spawnSync("powershell.exe", ["-Command", `Add-Type -AssemblyName PresentationCore,PresentationFramework; [System.Windows.MessageBox]::Show('${message.replace(/'/g, "''")}', '${title.replace(/'/g, "''")}', 0);`]);

The 0 at the end of the [System.Windows.MessageBox]::Show() method call specifies that the message box should have an OK button.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.