188

I have installed eslint in my machine and i have used visual studio code i have certain modules and process to be exported When i try to use "module" or "process" it shows it was working fine before.

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

and here is my .eslintrc.json

{

"env": {
    "browser": true,
    "amd": true

},
"parserOptions": {
    "ecmaVersion": 6
  },
"extends": "eslint:recommended",
"rules": {
    "no-console": "off",
    "indent": [
        "error",
        "tab"
    ],
    "linebreak-style": [
        "error",
        "windows"
    ],
    "quotes": [
        "error",
        "single"
    ],
    "semi": [
        "error",
        "always"
    ]
}

}

I want to remove this error

MaTHwoG
  • 1,911
  • 2
  • 7
  • 11
  • 10
    I have found it thanks. we need to add "globals": { "angular": false, "module": false, "inject": false, "document": false }, "env": { "browser": true, "amd": true, "node": true } in the .eslintrc.json file – MaTHwoG Apr 12 '18 at 06:42
  • Thanks :) This worked for me. You should put that as an answer with a quick example. – gin93r Jul 12 '18 at 12:33

6 Answers6

307

You are probably trying to run this in node environment.

The env section should look like this:

"env": {
    "browser": true,
    "amd": true,
    "node": true
},
Mihai Răducanu
  • 12,225
  • 4
  • 22
  • 31
151

In your ESLint config file, simply add this:

{
  ...
  env: {
    node: true
  }
  ...
}

That should fix the "module" is not defined and "process" is not defined error.

That assumes you are running in a Node environment. There is also the browser option for a browser environment. You can apply both based on your need.

If you want to prevent ESLint from linting some globals then you will need to add the specific global variables in the globals section of the config.

globals: {
  window: true,
  module: true
}
codejockie
  • 9,020
  • 4
  • 40
  • 46
80

You need to tell eslint that you are in a Node environment. My favourite way to do this for one-off files like gulpfile.js is to include this comment at the top:

/* eslint-env node */
Flimm
  • 136,138
  • 45
  • 251
  • 267
  • 11
    Perfect solution for frontend code, where globally specifying this is obviously wrong. Thanks! – qqilihq Nov 19 '20 at 23:19
  • 2
    This should be the accepted answer. Eliminates the problem in the most direct and objective way. – Adam Feb 01 '21 at 00:10
  • 2
    Accepted answer is way too broad, you don't want to allow node syntax everywhere, just in the one file. – Kyle Feb 18 '21 at 23:19
  • This is a very simple solution and also simply explained. – asim mehmood Aug 29 '22 at 12:07
  • Can't you just add `overrides` to your `eslintrc.cjs` to match all files that end in `*.cjs` to have `env: { node: true }` and then rename all your CommonJS files to end in `.cjs`? Is it bad? – Seangle Dec 18 '22 at 13:19
  • @Seangle If you have a lot of files, it is a valid option. but in this case, this is an easier option: the file itself decides how to deal with lint, and in a frontend project, you usually have only a couple of config files that may be written in the node module style – rfreytag May 01 '23 at 14:30
6

I think you can just rename all your CommonJS config files to have .cjs as their extension and then add this to eslintrc.cjs:

module.exports = {
  // ...
  env: {
    // If you don't want to change this to `node: true` globally
    es2022: true,
  },
    // then add this:
  overrides: [
    {
      files: ['**/*.cjs'],
      env: {
        node: true,
      },
    },
  ],
}
Seangle
  • 359
  • 2
  • 10
0

In my case below codes solve the issues

eslintrc.cjs

module.exports = {
  env: { browser: true, es2020: true,es2022: true },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:react/jsx-runtime',
    'plugin:react-hooks/recommended',
  ],
  overrides: [
    {
      files: ['**/*.cjs'],
      env: {
        node: true,
      },
    },
  ],
  parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
  settings: { react: { version: '18.2' } },
  plugins: ['react-refresh'],
  rules: {
    'react-refresh/only-export-components': 'warn',
    "react/prop-types": "off"
  },
}
0

in my .eslintrc.cjs

module.exports = {
  env: { browser: true,es2020: true },
   /*rest of code goes here*/
}

i've added node property to the env object and set it to true node: true so the final result would be

module.exports = {
  env: { browser: true, node: true, es2020: true },
   /*rest of code goes here*/
}
  

and now there's no issue

Ahmad ghoneim
  • 844
  • 7
  • 13