tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【node.js】npmで`SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode` というエラーで怒られる

はじめに

f:id:tweeeety:20180608202233p:plain

タイトルのまんまですが、
とあるプロジェクトでnpm runスクリプトを走らせると以下のようなエラーが出たのでその対処方メモ

$ npm run sample

> hoge@1.0.0 sample /Users/tweeeety/hoge
> gulp

/Users/tweeeety/hoge/node_modules/riot-compiler/node_modules/source-map/lib/source-map-generator.js:21
class SourceMapGenerator {
^^^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/tweeeety/hoge/node_modules/riot-compiler/node_modules/source-map/source-map.js:6:30)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)

npm ERR! Darwin 17.5.0
npm ERR! argv "/Users/tweeeety/.nodebrew/node/v5.12.0/bin/node" "/Users/tweeeety/.nodebrew/current/bin/npm" "run" "compile"
npm ERR! node v5.12.0
npm ERR! npm  v3.8.6
npm ERR! code ELIFECYCLE
npm ERR! hoge@1.0.0 compile: `gulp`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hoge@1.0.0 compile script 'gulp'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the hoge package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     gulp
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs hoge
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls hoge
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/tweeeety/hoge/npm-debug.log

このときの環境

$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]

$ node -v
v5.12.0

$ npm -v
3.8.6

$ rbenv -v
rbenv 1.1.1

$ nodebrew list
v5.12.0

current: v5.12.0

なにがおこったか

根本原因

githubのissueにあがってました

Version 0.7.0 of source-map fails to load on Node < 6 due to let without 'use strict'. This causes the following error:

node moduleのsource-map 0.7.0は
'use strict'を使用しないのでnodeのversion<6(6未満)にはロードできないらしい。
そのせいでエラーが発生するとのこと。

自分のところの原因

nodeが5.x.xでも、package.jsonでバージョンをがちがちに固定していれば大丈夫だと思います。

  • nodeが5.x.x
  • 固定していないmodule(今回でいうsource-map)のversionがあがって5.x.xでは動かなくなった

というかんじ

確認する

このプロジェクトで使っているsouce-mapを確認してみます。

# たしかに0.7.3がいる
$ npm list source-map
hoge@1.0.0 /Users/tweeeety/hoge
├─┬ gulp-concat@2.6.1
│ └─┬ concat-with-sourcemaps@1.1.0
│   └── source-map@0.6.1
├─┬ gulp-cssmin@0.2.0
│ └─┬ clean-css@3.4.28
│   └── source-map@0.4.4
├─┬ gulp-uglify@3.0.0
│ ├─┬ uglify-js@3.4.0
│ │ └── source-map@0.6.1
│ └─┬ vinyl-sourcemaps-apply@0.2.1
│   └── source-map@0.5.7
└─┬ riot@3.10.1
  └─┬ riot-compiler@3.5.1
    └── source-map@0.7.3

対応

記載の通りですが、
6以上にあげるとエラーはでなくなりました。

# インストールできるversionを確認
$ nodebrew ls-remote

# とりあえず少しだけあげて6.x.xにする
$ nodebrew install v6.14.2
$ nodebrew use v6.14.2
use v6.14.2

# エラーでない
$ npm run sample

ちなみに

5.x.xもものすっごく昔というわけではないですが、
nodeの5.x.xはサポートは終了しているようです。

特に理由がなさそうならあげちゃったほうがよさそうです。

おわり

5系は古い \(^o^)/