Run JavaScript in a Windows Command Line - javascript

This is something I only found out about today is that JavaScript can be run through a windows command line.
So I found out that to run a javascript file in windows cmd.exe you use cscript.
My hworld.js file only has one line
print('hello world');
I try to run this through the command line with
cscript /Prog/hworld.js
It didn't run with the error
Microsoft JScript runtime error: Object Expected
Are there steps i need to follow before simply cscript running a one line javascript file.
I was under the impression that JavaScript will just run out the box.
PS. Java is the development environment set up for the computer I am trying this on, installed and functional

The right command to print text in the console is
WScript.echo("your text");
The rest works pretty much like javascript.

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).

VS Code Terminal not running

when I go for console.log("hello world") my vs code terminal is not working unless I switch to java script debug terminal?
any one who know the solution please help
Try this ===>
1 Type node in terminal
2 console.log("Hello World")
Its pretty simple, just 2 clicks away
=> 1. Click on File
=> 2. Click on Auto Save
Option 1: Make sure that you installed Node.js After that, you need to type in Terminal window: node [whateverYourFilenameIs], click entre. //you type the word node, followed by your filename without the square brackets.
Option 2: Make sure you installed Node.js and install the VSCode extension Code Runner, restart VSCode, click on ctrl+Alt+N to run your file in terminal.
It is simple
write some code example: console.log("javascript");
ctrl +s
on terminal node and your js file name ...click enter ....and there will be no error

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

PhantomJS gives Segmentation fault when running any js using command line

I have installed Phantomjs 1.9.8 on my Centos 6.5 server.
From the command line if I type
phantomjs -v
I get the correct version number returned, so I presume that it is installed ok.
However if I create a simple javascript file, using the most basic example from the phantomjs example:
console.log('Hello, world!');
phantom.exit();
and save this to a file test.js, navigate to that folder via command line and run
phatomjs test.js
I get
PhatomJS has crashed... Segmentation fault.
Any idea what could be causing this or further tests I could run?
A simple solution - A key step of course is that you have to restart your apache web server to get this to work, I had forgotten this.
Leaving this up in case it helps anyone else stumbling along the same route

Eclipse Node Debugger require() exception

I am using windows machine with the latest eclipse.
I followed the instructions over https://github.com/nodejs/node-v0.x-archive/wiki/Using-Eclipse-as-Node-Applications-Debugger.
On the command line I execute node --debug-brk server.js. When I run eclipse debugger, it connects to the node. The first line in my code is require(), and when the debugger visits this line, it throws an exception:
line 1: uncaught JavaScript runtime exception: ReferenceError: "require" is not defined.
I tried to have a breakpoint after that line, but whenever the debugger starts, it visits the first (require()) line.
How can I pass this line, and continue debugging?
quote from https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger
Note that V8 engine debugger is not behaving very good when it steps over or steps into >require() method (it will crash), so try to set up first breakpoint past the initial module >loading. This will also enable you to set breakpoints in any of those modules as well.
try
node --debug server.js

Categories