ES6でのESLint

https://developer.cybozu.io/hc/ja/articles/360000098886-ESLint-JavaScript-%E3%82%B3%E3%83%BC%E3%83%89%E3%83%81%E3%82%A7%E3%83%83%E3%82%AB%E3%83%BC-

こちらの記事に従って、ESLintを使おうと思ってます。
ソースにlet宣言を多数使っているのですが、
“Parsing error: The keyword ‘let’ is reserved”,
が出力されます。

回避する方法を教えて下さい。

 

まちだ さん

こちらの記事で紹介している eslint は es5 という書き方をルールとして採用しており、
let は es6 (es5 より新しいルール)上の記法なので、その部分がエラーになっているのだと思います。

module.exports = {
"extends": "@cybozu/eslint-config/presets/kintone-customize-es5"
};

 

HANSA様

コメントありがとうございます。

es6に対応したルールセットは提示されていないという認識であってますでしょうか。

「様々なルールセットを利用する」に記載されている「ES2017での一般的なコーディング」や
「ES2017でのkintoneカスタマイズ」の通りに.eslintrc.jsを書き換えてみましたが、実行すると
以下のエラーが出力されます。(es5対応の場合は正常にeslintが動作します)

ESLint: 6.1.0.
ESLint couldn't find the plugin "eslint-plugin-import".
(The package "eslint-plugin-import" was not found when loaded as a Node module from the directory "C:\ソースディレクトリ".)

npm ls -g --depth=0を実行すると以下のエラーが出ています。
ES2017のルールセットを利用するには、追加でインストールする必要があるのでしょうか。解決方法がありましたらお願いします。

+-- @cybozu/eslint-config@6.0.1
+-- eslint@6.1.0
+-- prettier@1.18.2
`-- typescript@3.5.3

npm ERR! peer dep missing: eslint@^5.0.0, required by @typescript-eslint/eslint-plugin@1.13.0
npm ERR! peer dep missing: eslint@^5.0.0, required by @typescript-eslint/parser@1.13.0

まちだ さん

ご認識の通り、ルールセットを変更することで、EC2017 (= es8) までの書き方に対応できます。

また、該当のエラーは ソールディレクトリ内で npm install --save-dev eslint-plugin-import と実行することで解消できないでしょうか?

HANSA様

早速のご返信、本当にありがとうございます。
ひとりで作業をしておりますので、ご返信いただけると心強い限りです。

ソースのあるディレクトリにてnpm install ~を実行したところ、以下のワーニング(といってもsaveError)
となりました。
npm ls -g --depth=0 の結果は前述と同じです。

C:\ソースディレクトリ>npm install --save-dev eslint-plugin-import
npm WARN saveError ENOENT: no such file or directory, open 'C:\ソースディレクトリ\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'C:\ソースディレクトリ\package.json'
npm WARN eslint-plugin-import@2.18.2 requires a peer of eslint@2.x - 6.x but none is installed. You must install peer dependencies yourself.
npm WARN tmp No description
npm WARN tmp No repository field.
npm WARN tmp No README data
npm WARN tmp No license field.

+ eslint-plugin-import@2.18.2
added 53 packages from 35 contributors and audited 98 packages in 7.454s
found 0 vulnerabilities


C:ソースディレクトリ>npm ls -g --depth=0
C:ホームディレクトリpm
+-- @cybozu/eslint-config@6.0.1
+-- eslint@6.1.0
+-- prettier@1.18.2
`-- typescript@3.5.3

npm ERR! peer dep missing: eslint@^5.0.0, required by @typescript-eslint/eslint-plugin@1.13.0
npm ERR! peer dep missing: eslint@^5.0.0, required by @typescript-eslint/parser@1.13.0

eslint を実行すると異なるエラー表示となってしまいました。

Error: Failed to load plugin 'import' declared in 'PersonalConfig » @cybozu/eslint-config » ./lib/base.js': Cannot find module 'eslint'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (C:\ホームディレクトリpmode_modules\eslintode_modules\v8-compile-cache\v8-compile-cache.js:161:20)
at Object.<anonymous> (C:\ソースディレクトリode_modules\eslint-plugin-import\lib\ExportMap.js:20:15)
at Module._compile (C:\ホームディレクトリpmode_modules\eslintode_modules\v8-compile-cache\v8-compile-cache.js:192:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)

ホームディレクトリでnpm install --save-dev eslint-plugin-importを実行してしまい、その後ソースディレクトリで再度実行しました。
2度実行したことで問題が生じたのでしょうか。

まちだ さん

すみません!
記事ではすべてのモジュールをグローバルにインストールしていたのですね。

であれば、 eslint-plugin-import もグローバルにインストールする必要がありそうですね。

ソースディレクトリで以下のコマンドを順番に実行してみてください。

npm uninstall eslint-plugin-import

npm install -g eslint-plugin-import

HANSA様

長々とお付き合いいただきありがとうございました。
グローバルインストールし、NODE_PATHを設定することでうまく動作しました。

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。