Why nodejs console.log only showing " ... "? - node.js

I have this in helloworld.js :
console.log('Hello World');
and the output is :
http://img856.imageshack.us/img856/4152/8vh3.png

You're currently using REPL of node.js.
You should run such command directly in CMD.
By the way, you can input raw code, like console.log('Hello') in REPL.

This should be run directly from command line interface as mentioned by #Chichou. If you want to test such things you can also use browser console.

Related

Interract with a .exe program with js instead of typing in the program

Hello I'm tryna make a Skyrim server Dashboard.
The server look like this =>
On this server i can type some command like this =>
when I manualy wrote /help and it show the output.
I tried to run the executable in node js, the server is working, I can join it, And I can see the output on my VSCode Terminal
But I can't input some text or command
Hope you can help me thanks in advance.
##Its my first ask
Add shell: true to the spawn method so you can still pass commands to the process.
const child = require('child_process').spawn("C:/SkyrimTogetherServer.exe", {
shell: true
});

Running MacOS shortcuts shell command from node script

I am trying to run MacOS shortcuts via NodeJS script. To achieve that I created shortcuts in the MacOS Shortcuts app, which normally run in the terminal (ZSH) by typing shortcuts run shortcutname (Docs). They work fine when typing them directly into the terminal, but not when called from a NodeJS script.
My script looks like this:
exec('shortcuts run shortcutname', (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
When starting the script from the terminal node script.js it does nothing. It does not return anything nor does it finish. I tried replacing the command shortcuts run shortcutname by other commands the system should know. For example ls, or git. They all work. Also when I leave out the shortcutname in the end, it complains about the missing parameter.
I was suspecting missing access rights, but apparently NodeJS runs as my normal user. Running whoami in script context returns my user name.
NODE_DEBUG=cluster,net,http,fs,tls,module,timers node script.js did not show anything unusual. I also tried using the full path of the binary. which shortcuts gave me /usr/bin/shortcuts. Which I used in my script /usr/bin/shortcuts run shortcutname.
What am I missing? Thanks in advance!
Though I thought I tested it, I found the solution:
execSync instead of exec works
It’d be cool if someone could explain why exec wouldn’t work.
Cheers!

How can I use readline-sync npm repository with WebStorm?

I wrote the following code using 'readline-sync' dependency.
var readlineSync = require('readline-sync');
function main() {
printMenu();
var userName = readlineSync.question('Please enter your choice:');
console.log(userName);
}
main();
I ran this code from WebStorm trying to use the WebStorm console window.
I got the error:
Error: The current environment doesn't support interactive reading
from TTY. stty: when specifying an output style, modes may not be set
When I run it from linux terminal the code works with no error. I understand from the error message that 'readline-sync' cannot work from WebStorm console. Do you have any idea how to solve it?
I found out the answer.
type in WebStorm terminal:
$ node --debug-brk
Web storm will give you the port number the debugger is listening to. On my machine it was 5858. Then press 'Ctrl+C'.
Create a new debugging configuration as the following one:
In WebStorm terminal type again: "$ node --debug-brk main.js "
put a breakpoint somewhere.
Click the debugging icon
Happy Debugging!

node.exe does not read file, but returns ellipsis (...)

End feb 2014 I downloaded node to c:\dev\0.10\ of my windows 7 machine, and node.exe opens fine.
Inspired by Smashing Node (some book), I want to achieve the following:
Firefox shows the plain text Smashing Node! if i point it to localhost:3000 and run in the node console
node my-web-server.js
where my-web-server.js file next to node.exe contains
require('http').createServer(function(req,res){res.writeHead(200, ('Content-Type', 'text/html'));res.end('<marquee>Smashing Node!</marquee>');}).listen(3000);;
but I fail: browser says
cannot connect with webserver on localhost:3000.
If paste the above oneliner in node it reacts with
{ domain: null,
_events: ..etc... }
Ignoring that, browser F5 results in Smashing Node!.
Node refuses the simplest of files, say have a file called hello.js next to node.exe and file contains the ascii text
console.log("hello");
i type:
node hello.js
node returns
...
(an ellipsis in dark gray)
Expected was: node returns hello
i type a file that does not exist like this:
node die
node returns
...
if i type
var http = require('http');
node:
undefined
(in a darkish gray) Expected was: something like ok, especially since the above oneliner resulted in a web server.
If however i type
npm install colors
node reacts with
npm should be run outside of the node repl, in your normal shell. (Press Control-D to exit.)
of course
node --version
also responds with an ellipsis.
What can i do?
How do i make node to process files?
You need to run node hello.js (and npm) on your command line shell (e.g. cmd.com or Windows PowerShell).
You are trying to run it in the Node REPL (node console), where you are expected to type only JavaScript.
It's hard to tell where the Smashing Node failed, particularly as a one-liner. The code runs OK on my machine, but you can split the code as follow and add a few console.log() to see how is executing:
console.log('about to start listening for requests');
require('http').createServer( function(req,res) {
console.log('a request was received', req.url);
res.writeHead(200, ('Content-Type', 'text/html'));
res.end('<marquee>Smashing Node!</marquee>');
}).listen(3000);;
console.log('listening for requests');
Save this code as my-web-server.js as you did before and then run from the command line "node my-web-server.js" and point your browser to localhost:3000
Also, I've got a very basic Node.js tutorial that might help you with the basics: http://hectorcorrea.com/#/blog/introduction-to-node-js/51

Running 'casperjs test' in phantom

So I have a file running in node which runs a local copy of PhantomJS as below shows:
phantom.casperPath = 'node_modules/casperjs';
phantom.injectJs('node_modules/casperjs/bin/bootstrap.js');
var casper = require('casper').create({
viewportSize: config.viewportSize
});
casper.test.begin('Runing tests here', 5, function suite(test) {
// Tests here
});
Without the casper.test.begin() my tests function fine. I have the correct version 1.1.0 which can use this test suite but I get the following error in my console:
CasperError: casper.test property is only available using the `casperjs test` command
The CasperJS docs mentions this too: http://docs.casperjs.org/en/latest/testing.html. My question is how do I run this Casper under this command in the above code so I can use these tests?
Thanks!
CasperError: casper.test property is only available using the casperjs test command
problem solved.
You have to include this line at the top of your script in your xyz.js, so that the .test property becomes true;
phantom.casperTest = true;
Then you should have no problem Launching from the terminal:
casperjs xyz.js
you can also call casperjs test xyz.js
For more info, check the doco here : http://docs.casperjs.org/en/latest/testing.html

Resources