I was just wondering, if it's possible to use ES6 in Node 10.15 now in 2019, because I thought, ES6 would now be a natively supported and implemented Javascript feature? I found some answer here: NodeJS plans to support import/export es6 (es2015) modules but I wasnt sure what the actual status is now.
I just tried out some ES6 classes with arrow functions in Node:
class Test {
testVar = 1;
constructor(x,y) {
this.counter =0;
this.x = x;
this.y = y;
this.increaseCounter();
this.testVar +=1;
}
getCounter = () => {
console.log("Counter:", this.counter);
}
increaseCounter = () => {
this.counter += 1;
}
}
I get an error:
getCounter = () => {
^
SyntaxError: Unexpected token =
and also, I cannot create class instance variables that are global to the class (and increase testVar by 1 every time a new class instance is created..) How is that normally done in Javascript classes?
I know there is a babel piler package out there that supports this and transpiles the code somehow, but should ES6 not be natively supported Javascript code by now?
I was just wondering, if it's possible to use ES6 in Node 10.15 now in 2019, because I thought, ES6 would now be a natively supported and implemented Javascript feature? I found some answer here: NodeJS plans to support import/export es6 (es2015) modules but I wasnt sure what the actual status is now.
I just tried out some ES6 classes with arrow functions in Node:
class Test {
testVar = 1;
constructor(x,y) {
this.counter =0;
this.x = x;
this.y = y;
this.increaseCounter();
this.testVar +=1;
}
getCounter = () => {
console.log("Counter:", this.counter);
}
increaseCounter = () => {
this.counter += 1;
}
}
I get an error:
getCounter = () => {
^
SyntaxError: Unexpected token =
and also, I cannot create class instance variables that are global to the class (and increase testVar by 1 every time a new class instance is created..) How is that normally done in Javascript classes?
I know there is a babel piler package out there that supports this and transpiles the code somehow, but should ES6 not be natively supported Javascript code by now?
Share Improve this question asked Mar 13, 2019 at 8:32 MarcLMarcL 3,5938 gold badges57 silver badges107 bronze badges 5-
2
getCounter = () => {
in this context, isn't valid ES6 for a start – Jaromanda X Commented Mar 13, 2019 at 8:35 -
but it is valid Stage 3 Proposal - you can enable this in node using the mand line flag
--harmony
– Jaromanda X Commented Mar 13, 2019 at 8:49 -
@JaromandaX is that method added to
Test.prototype
or every instance of the class directly? – adiga Commented Mar 13, 2019 at 8:58 - @JaromandaX with --harmony flag it works with the arrow functions. – MarcL Commented Mar 13, 2019 at 9:02
-
yes, I know that @user2774480 ... but that still isn't ES6 i.e.
ES2015 (6th edition)
... it may beES2019 (10th edition)
or later, because it's not inES2018 (9th edition)
- ECMA did not stopped developing javascript in 2015 ... there's been 3 more specs released since – Jaromanda X Commented Mar 13, 2019 at 9:09
2 Answers
Reset to default 4Can I use ES6 Javascript in Node.js without Babel?
Yes, you can, Node supports all JS (ECMAScript) features up to ES2018 : https://node.green/
You should create your methods like this :
class Test {
testVar = 1;
constructor(x, y) {
this.counter = 0;
this.x = x;
this.y = y;
this.increaseCounter();
this.testVar += 1;
}
getCounter() {
console.log("Counter:", this.counter);
}
increaseCounter() {
this.counter += 1;
}
}
No need to create an attribute for the only purpose being holding an anonymous arrow function.
as of Nodejs 11.11.0, class fields are not part of the ECMAscript spec .... yet .. they are a stage 3 proposal
You can enable some (all?) of such proposed features by using the --harmony
mand line argument
Looking at the current state of play, class fields don't seem to be part of ES2019 (10th edition)
either. Whilst I can't find that document (can only find up to ES2018 (9th edition)
, the table in https://node.green/ shows class fields in a section titled ESnext
which is after the section ES2019