201

I am using ESLinter for a simple node project. Below is the only code I have in index.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send({
        hi: 'there'
    });
});

const PORT = process.env.PORT || 5000;
app.listen(PORT);

I am using VSCode editor. It automatically runs ESLint for JS code.

In the IDE, I see below error for last but one line -

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

Any Idea what's wrong?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
ArunKolhapur
  • 5,805
  • 4
  • 18
  • 31
  • does ESLINT have some way of informing it you are linting nodejs code? – Jaromanda X Jun 17 '18 at 05:37
  • 7
    You probably haven't configured eslint correctly. https://eslint.org/docs/user-guide/configuring#specifying-environments – Felix Kling Jun 17 '18 at 05:38
  • 1
    Thanks @FelixKling - It was so dumb of me not looking into .eslintrc.json file. I had accidentally selected `browser default global variables`. Your link hinted me to that. Now, the error is gone! – ArunKolhapur Jun 17 '18 at 05:42

10 Answers10

315

When I got error I had "browser": true instead of "node": true.

I have fixed this with following config for .eslintrc.json file-

{
    "env": {
        "node": true,
        "commonjs": true
    },
    "extends": "eslint:recommended",
    "rules": {
        "indent": [
            "error",
            "tab"
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ]
    },
    "parserOptions": {
        "ecmaVersion": 2015
    }
}

Thanks @FelixKling and @Jaromanda X for quick responses.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
ArunKolhapur
  • 5,805
  • 4
  • 18
  • 31
101

Adding "node": "true" to an existing list of environments will do the job, too

"env": {
        "node": true,
        "commonjs": true,
        "browser": true,
        "es6": true
       }
crtag
  • 2,018
  • 2
  • 14
  • 13
58

Add .eslintrc file to root of your project (if you don't already have one) and define globals to ignore

{
    "globals": {
        "process": true
      }
}

Make sure you use process.env though out the project but only in a single configuration file. Consider adding no-process-envrule.

https://eslint.org/docs/rules/no-process-env

t-reksio
  • 3,251
  • 18
  • 12
35

If you have eslint installed, add env: { node: true } to the .eslintrc.js file

youngwolf
  • 461
  • 5
  • 4
11

This config helped me and can help others too.

{
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true,
      "modules": true
    },
    "ecmaVersion": 2020,
    "sourceType": "module",
    "useJSXTextNode": true,
    "warnOnUnsupportedTypeScriptVersion": false
  },
  "root": true,
  "env": {
    "browser": true,
    "es6": true,
    "node": true,
    "commonjs": true
  },
  "extends": [ "eslint:recommended"],
  }
}
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
Paulo Andrade
  • 178
  • 1
  • 5
7

When working on the node js project. This might help you. It is working on my end.

module.exports = {
    "env": {
        "node": true,
        "browser": true,
        "commonjs": true,
        "es6": true
    },
    "extends": "eslint:recommended",
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parserOptions": {
        "ecmaVersion": 2018
    },
    "rules": {
    }
};
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
2

If you are getting the error even after settng "node": true, remember to install @types/node with

npm i -D @types/node

yarn add -D @types/node
tHeSiD
  • 4,587
  • 4
  • 29
  • 49
0

Add these env lines in your .eslintrc.js file.

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "node": true,
        "es2021": true,
        "jest": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
        "ecmaVersion": "latest"
    },
    "rules": {
    }
}
M. Hamza Rajput
  • 7,810
  • 2
  • 41
  • 36
0

In the new ESLint configuration file eslint.config.js, the env property has been removed so you should use the languageOptions.globals property:

import globals from 'globals';

export default [
  {
    languageOptions: {
      globals: {
        ...globals.browser
      }
    }
  }
];
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
-6

Another remedy for this issue is to simply import process from "process".

Even though it's not strictly necessary, one could argue it's better practice anyway.

P Varga
  • 19,174
  • 12
  • 70
  • 108