17

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"]
}

2

2 Answers 2

28

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Yes, I was including the no-undef for testing purposes and I have removed it now. THANKS!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.