LoginSignup
19
17

More than 5 years have passed since last update.

node で ES6 の let を使ったファイルを実行すると SyntaxError になるのを回避する

Last updated at Posted at 2015-10-11

(node v4.1.2)

let 使うと SyntaxError になる現象

ES6 の記述を使って node コマンドでファイルを指定して実行できます。

例えばアロー関数を使って実行

hoge.js
var a = (num) => {
  console.log(num);
};
a(10
$ node hoge.js

10

しかし、なぜか let 使うと SyntaxError になります

hoge02.js
let foo='bar';
console.log(foo);

$ node hoge02.js

/Users/cortyuming/example/hoge02.js:1
let foo='bar';
^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

回避

その1

"use strict" をつけるとエラー出なくなる

hoge03.js
"use strict"
let foo='bar';
console.log(foo);
$ node hoge.js

bar

その2

実行時に --use_strict オプションをつける

$ node --use_strict hoge02.js

bar

参考リンク

19
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
17