The question is published on by Tutorial Guruji team.
How can I run a short ES6 (Javascript) program without semicolons?
I have the following code in a jstry.js
file
let v = 1 console.log(v)
I open a CMD window in windows 10, run it and get an error that a semicolon is expected. What can I change (without babel) so that this code is run OK?
Remark: I understand this is supposed to be supported in ES6, and that in actuality it was even supported before but… now comes some explanation that I don’t really understand: until ES6 came along and then something (that I don’t understand either) happened.
My path is:
PATH=C:ProgramDataOracleJavajavapath; C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem; C:WINDOWSSystem32WindowsPowerShellv1.0; C:Program Files C:UsersuserAppDataLocalAndroidSdktools; C:Program Filesnodejs; C:Program FilesJavajdk1.8.0_172bin; C:Program FilesMicrosoft VSCodebin; C:UsersuserAppDataRoamingnpm; C:UsersuserAppDataLocalProgramsMicrosoft VSCodebin;
So maybe running in the cmd window is invoking nodeJS? I’m running 8.11.1
Answer
Your code is perfectly valid in ES6 (note: there’s no thing called ECMA6, it’s ECMAScript 6 or abbreviated as ES6).
A problem might be that if you run this script by entering its filename only (not node
filename
), that it is run by the Windows Script Host (cscript.exe
or wscript.exe
), which is the default way of running .js
files on Windows; however, it doesn’t support ES6.
To solve this issue, you have multiple ways:
- Always run scripts by prepending its filename by the name of the program, in this case,
node
, or - Change the default program associated with the file extension
.js
to Node.js (here‘s how to do it)