0

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

FZs
  • 16,581
  • 13
  • 41
  • 50
pashute
  • 3,965
  • 3
  • 38
  • 65
  • Are you running this with Node? Babel is not necessary, but you need a consistent JS execution environment first. – loganfsmyth Jul 06 '20 at 15:27
  • I'm running it in CMD in windows 10 . I'll update the question with my path, which will probably tell me what is running the .js file. No? – pashute Jul 06 '20 at 16:06
  • 1
    Your code is perfectly valid. A problem might be that if you run this script by entering its filename only (not `node filename`), that it is run by `cscript.exe` or `wscript.exe`, which doesn't support ES6. – FZs Jul 07 '20 at 08:26
  • @FZs Could you make that an answer and I'll mark it. That was it/ – pashute Jul 14 '20 at 15:19
  • I've posted it. – FZs Jul 14 '20 at 16:36

1 Answers1

1

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)
FZs
  • 16,581
  • 13
  • 41
  • 50
  • 2
    I'd recommend changing the default program for `.js` files to an editor. Accidentally running scripts you might have lying around can cause quite some issues. – Bergi Jul 14 '20 at 17:59