16

I am setting up a new project as described in the typescript-eslint getting started docs. However, in my .eslintrc.js file I am getting an error:

'module' is not defined.eslint(no-undef)

Now, if I remove eslint:recommended from the extends of the config, this error goes away. However, typical rules like debugger or const iAmUnused = true do not get picked up by ESLint, so if feels a little like whack-a-mole.

Why is my ESLint file being picked up when it's in the root of my project with the eslint:recommended enabled? I don't want to include this file in my .eslintignore because when running my eslint command, it says this file is already automatically ignored, but it's not ‍♂️

ESLINTRC:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: '*/tsconfig.json',
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
  plugins: ['@typescript-eslint', 'jest', 'react', 'react-hooks'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:jest/recommended',
    'plugin:prettier/recommended',
    'plugin:react/recommended',
    'plugin:react-hooks/recommended',
    'prettier',
    'prettier/@typescript-eslint',
  ],
  rules: {
    'no-unused-vars': 2,
  },
  env: {
    browser: true,
    es6: true,
    jest: true,
  },
  overrides: [
    {
      files: ['**/*.tsx'],
      rules: {
        'react/prop-types': 'off',
      },
    },
  ],
};

TSConfig:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "declaration": true,
    "declarationDir": "build",
    "jsx": "react",
    "lib": ["es6", "dom", "es2016", "es2017"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "rootDir": "./src",
    "rootDirs": ["./src"],
    "sourceMap": true,
    "strict": true,
    "target": "es5"
  },
  "include": ["./src"],
  "exclude": ["node_modules", "build", "dist", "src/**/*.stories.tsx", "src/**/*.test.tsx"]
}

Phil Lucks
  • 2,704
  • 6
  • 31
  • 57
  • 1
    You haven't specified that `module` is a global. I'm curious how you're using it though because you would typically only do that if you were writing CommonJS modules by hand. – Aluan Haddad Aug 19 '20 at 01:59
  • Does this answer your question? [module is not defined and process is not defined in eslint in visual studio code](https://stackoverflow.com/questions/49789177/module-is-not-defined-and-process-is-not-defined-in-eslint-in-visual-studio-code) – Brian Ortiz Oct 06 '21 at 19:38

2 Answers2

27

Looks like the env needed to be updated from:

env: {
    browser: true,
    es6: true,
    jest: true,
  },

to include node: true for the module error to be resolved.

Phil Lucks
  • 2,704
  • 6
  • 31
  • 57
2

https://eslint.org/docs/user-guide/configuring#specifying-environments

You need to specify the environment(s) that are relevant to your project.

In this case, you would probably want to add the commonjs environment.

I would just consider turning off the no-undef rule though, as it's a check that's already covered by TypeScript itself.

Brad Zacher
  • 2,915
  • 18
  • 31