Search code examples
javascriptecmascript-6semicolon-inference

How can I run ES6 semicolumnless code in a windows 10 cmd window without babel?


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:\ProgramData\Oracle\Java\javapath;
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\Program Files C:\Users\user\AppData\Local\Android\Sdk\tools;
C:\Program Files\nodejs\;
C:\Program Files\Java\jdk1.8.0_172\bin;
C:\Program Files\Microsoft VSCode\bin;   
C:\Users\user\AppData\Roaming\npm;
C:\Users\user\AppData\Local\Programs\Microsoft VSCode\bin;

So maybe running in the cmd window is invoking nodeJS? I'm running 8.11.1


Solution

  • 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 nodefilename), 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)