LoginSignup
4
1

More than 3 years have passed since last update.

eslintを実行した際に発生する'module' is not definedのエラーの消し方

Last updated at Posted at 2021-04-29

トラブルの内容

  • eslintを実行したときに'module' is not definedと表示されてしまうが、このエラーの直し方がわからない。

作業環境

  • Mac OS
  • VS Code
  • node v12.15.0
  • npm 6.14.8
  • eslint 6.14.8

トラブルの発生状況

  • 初めてeslintを使うというレベルのユーザが対象です。
  • eslintをWebを見ながら設定をしていき、いざeslintを実行をします。
// eslintの実行
./node_modules/.bin/eslint *.js
  • すると、以下のエラーが表示されます。
/Users/[ユーザー名]/[作業ディレクトリ]/.eslintrc.js
  1:1  error  'module' is not defined  no-undef

✖ 1 problem (1 error, 0 warnings)

  • これは、eslintrc.jsの1行目に書かれているmodule.exports = {の部分でエラーが発生しています。

解決方法

  • .eslintrc.jsのファイルを以下のように変更すれば解決します。
  • [-]の行を削除して、[+]の行を追加して下さい。
module.exports = {
  env: {
    browser: true,
[-] es2021: true
[+] es2021: true,
[+] node: true,
  },
  extends: "eslint:recommended",
  parserOptions: {
    ecmaVersion: 12,
    sourceType: "module",
  },
  rules: {},
};

  • 再度、eslintを実行して、エラーが表示されないことを確認します。
  • 以上です。

その他

別のコマンドでeslintを実行する方法

  • 以下のコマンドでもeslintを実行できます。
npm run lint
  • ただし、このコマンドでeslintを実行させるためには、packeage.jsonを以下のように編集する必要があります。
{
 〜省略〜
  "scripts": {
[-]    "test": "echo \"Error: no test specified\" && exit 1"
[+]    "test": "echo \"Error: no test specified\" && exit 1",
[+]    "lint": "eslint *.js"
  },
  〜省略〜
}

参考サイト

4
1
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
4
1