Console.log not displaying any output (NodeJs) - javascript

I am trying to console log a piece of JavaScript code through Node.Js but the terminal is not showing any output.
const pokemon = require('pokemon');
pokemon.all();
var name = pokemon.random();
console.log(name);
Can't even console log a simple line of code
console.log("Hello");
Image of terminal after executing node file

It is possible that you use node which is not a nodejs package but another. If you type node on command line, do you see something like this? Especially the message Welcome to Node.js vxx.xx.x.
hanania#laptop:~$ node
Welcome to Node.js v12.16.3.
Type ".help" for more information.
>

just try to check out if nodejs is installed successfully on your device by runing
node --version
and if it doesn't respond with nodejs version you need to reinstall nodejs

Related

Why do I get a bash: syntax error near unexpected token on every bit of code with VS Code?

I am very new to coding, and have just installed VS Code, and installed Node.js as well as Git Bash. I was working on a project, but couldn't console.log anything as I always received the following error when using Git Bash:
bash: syntax error near unexpected token `validateCred' (One of the
variables I used)
I created a new JS file, and ran the following code:
const hello = 'hello world';
I still received the same bash error. I have tried replacing my code with code that I know works and still receive the same errors.I have also tried using Powershell as my terminal, but receive the following errors every time:
batch : The term 'batch' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
I am at a loss, and I'm sure it's something really simple, but I can't seem to figure it out!
A Bash shell expects you to enter Bash code and not JavaScript code.
If you want to run JavaScript code then you need to run it in Node.js and not in Bash.
Generally, the command node will launch Node.js in a Bash shell. (Assuming it is installed on the $PATH).

How can I use ffplay from Electron.js app?

I've installed ffplay in my working folder (in bin subfolder) using ffbinaries (ffbinaries downloader). My current platform is linux-64.
I've use:
var spawn = require('child_process').spawn,
player = spawn('./bin/ffplay', ['http://path_to_video_file']);
but got an error in terminal stderr:
./bin/ffplay: error while loading shared libraries: libSDL2-2.0.so.0: cannot open shared object file: No such file or directory child process exited with code : 127
How can I get access from my javascript code to this binary for playing videos or how can I get ready-to-use binary which is a built-in for my Electron app?
...Or how can I get all of ffplay possibilities for playing videos inside Electron app?
Thanks in advance!
The error you get means that ffplay cannot find libSDL.
First, make sure the library is installed by opening a terminal window and typing:
sudo apt install libsdl2-dev
If it wasn't installed, try to run your program again after it was installed.
If you still have the problem, type the following in your terminal window:
export LD_LIBRARY_PATH="/usr/local/lib"
Try again to run your program. If the problem is now solved, edit the file etc/environment and add the setting there to make it permanent:
sudo nano /etc/environment
Add this LD_LIBRARY_PATH="/usr/local/lib" at the end, exit and save.
Hope it helps.

node js returning Syntax error: Unexpected identifier

I downloaded and installed node.js on Windows and I'm following a simple tutorial from nodebeginner.org.
I've created a file called HelloWorld.js which contains just:
console.log("Hello World");
When I type node HelloWorld.js in the node.js console I get:
SyntaxError: Unexpected identifier
I checked my classpath variable and it has the C:\Program Files\nodejs\ on it.
HelloWorld.js is still open in Notepad++ for editing.
What am I doing wrong?
I think you are already in the the console.
Just follow the steps to fix the error:
1) Try doing CTRL + C couple of times. See if you exit the console
2) Then do node HelloWorld.js
I think you will get your output
When in your node console already, you can simply do require("./HelloWorld.js") to get the output. (Given that you are in the directory that contains this file)
When I type node HelloWorld.js in the node.js console I get
You should type JavaScript into the Node.js console.
node is a program name. HelloWorld.js is a command line argument. They are not JavaScript.
You should type those into your shell (e.g. Windows Powershell or bash).
I had the same issue when following an online course, my mistake was that i did not safe the file i was following as .js in the name when saving.
Therefore my Hello.js did not open because it was only Hello
If people are facing below-mentioned error: Uncaught SyntaxError: Unexpected identifier at the time of running, console.log("Hello World"); code with command, node HelloWorld.js, in VS code editor
Problem :
node HelloWorld.js ^^^^^ Uncaught SyntaxError: Unexpected identifier
Solution :
(1) Just install Babel JavaScript extension in VS code editor
(2) After Babel JavaScript extension is installed, save your Program and then run your program with the command, node HelloWorld.js
Definitely will get the expected result.
I'm on linux and I'd the same issue
what I was writing in terminal is :
node
node file.js
all what I had to do is to write node file.js from the start without writing node first .
Although the question is old, I just solved it. So for anyone who still likes an answer: Apparently Node.Js installs two different consoles or executables. There is "Node.js" and there is "Node.js command prompt". Use the latter and it will work
To clarify, I used another tutorial in Dutch. Use the Javascript code in there and then in your web browser type http://localhost:3000. There you will see the Hello World output.
A little late but I figured this out as I'm learning it as well. You are not in the correct Node.js command window:
You are probably trying to run Node.js, ie. the one with the red arrow. This gives you the "Unexpected identifier" error. You want the Node.js command prompt, or the one shown with a green arrow.
Craig
On windows hit CTRL + D to exit REPL and then run HelloWorld.js again

where to find console in Node-RED

I can't find the console for Node-RED.
Do I have to use Node-RED Runtime API's RED.log()? Some Node-RED frontends, like FRED, seem to have a console. Also here a console is mentioned.
Is there a console accessible on the shell, or do I have to use the debug-Node?
Thanks in advance
Peter
Node-RED outputs data to the terminal it is started on.
So if you start it manually on the command line with node-red or node red it will print to the terminal.
If you've installed Node-RED as a service so it is launched at startup then it will depend on what platform you are on as to where the console log will end up. On Linux then there is a fair chance that it will end up in the journal and can be tailed with the following command:
sudo journalctl -f -u nodered -o cat
If you're running Node-RED on Bluemix then the you can see the console with the cf command line too:
cf logs <app-name>
or you can see it in the Bluemix web console.
As for sending data to this output then you have a few of options.
The Debug node has an option to output to the console, this is useful when the msg object (or msg.payload) is too big to fit in the debug side bar.
When writing your own nodes then you can use RED.log, This will add a time stamp and a node id to the output which can be useful when debugging.
At a pinch you can use console.log, but this will just dump things to standard out with no formatting or time stamp

where do I enter my node commands and why?

Node n00b here. I just installed node on my windows desktop and I'm wondering where I should enter my node commands and why... I have three options (see below). Oh, and if one of these (i.e. node.exe) is not intended for entering node commands, what is it for?
I have looked at the nodejs.org docs and I don't see a clear overview/explanation of what each of these are for and why it's recommended to use one over the other.
Thanks for any insight.
================================================
1) Windows Command Line:
2) Node.js command prompt:
3) Node exe
#1 This is simply window's cmd, you can type node --help there to get a general overview of what you can do with node
#2 Also cmd but with some extra configuration, when you view the properties you'll see it's; C:\Windows\System32\cmd.exe /k "C:\Program Files\nodejs\nodevars.bat" Which basically means it runs cmd with a .bat script, that sets some environment variables, changes the title and prints some welcome message.
#3 This is NodeJS' REPL which evaluates JavaScript within NodeJS' context. ( This is the same as running node from cmd )
Usually you don't enter your code directly, but put it in a file instead. Create this file with the name hello.js:
console.log("Hello World!");
Switch to the directory of your file:
cd C:\Users\yourname\yourdirectory
Then run it with the node command:
node hello.js
And you should get the following output:
Hello World!

Categories