Since const and let are new syntaxes, they must be rewritten with the only binding mechanism that was available before ES6: var . In this case, var allows all sorts of random reassignments without any warning.
Therefore, when you enter an expression in babel-node , babel translates it, evaluates it, and then displays the result. Babel can check for improper use of the const binding during forwarding, so you see an error for const a = 1; let a = 2 const a = 1; let a = 2 . But const a = 1 and let a = 2 , when transpiled / are evaluated as separate expressions, will not throw an error, because babel cannot detect the problem in only one expression.
A more vivid demonstration of the problem: for each expr expression, enter babel-node REPL, this is essentially what happens
evaluate(transpile(expr))
Thus, you will not see an error here.
evaluate(transpile('const a = 1')) evaluate('var a = 1') // bind a to 1 // return undefined evaluate(transpile('let a = 2')) evaluate('var a = 2') // bind a to 2 // return undefined
But you will see an error here.
evaluate(transpile('const a = 1; let a = 2'))
naomik
source share