I am using node v6.0.0 and wanted to use ES2016 (ES6). However I realized that the "import" syntax is not working. Isn't "import" fundamental to for writing modular code in ES2015? I tried running node with --harmony_modules
option as well but still got the same error about "import". Here's the code.
Working code without "import":
'use strict';
let sum = 0;
class Number {
addNumber(num1, num2) {
return num1 + num2;
}
}
let numberObj = new Number();
sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);
Notworking code with "import":
server.js
'use strict';
import Number from "./Number";
let sum = 0;
let numberObj = new Number();
sum = numberObj.addNumber(1,2);
console.log("sum of two number 1 and 2 "+ sum);
Number.js
'use strict';
export default class Number {
addNumber(num1, num2) {
return num1 + num2;
}
}
I also checked http://node.green/ to see the supported es6 however not able to understand why it doesn't work with --harmony_modules option. Please help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…