ESLint - 'process' is not defined
Asked Answered
F

12

221

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?

Fall answered 17/6, 2018 at 5:35 Comment(3)
does ESLINT have some way of informing it you are linting nodejs code?Europe
You probably haven't configured eslint correctly. eslint.org/docs/user-guide/configuring#specifying-environmentsCzardas
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!Fall
F
349

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.

Fall answered 17/6, 2018 at 5:47 Comment(4)
This got rid of my ESlint 'URL' is not defined, too.Ingar
In my case I was missing the node: true on my env eslint config file.Lovettalovich
Adding the "node" : true worked for me also with this issue.Golda
Thought I selected "node" in the wizard at npm init @eslint/config but seems I didn't hehAltercation
G
109

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

"env": {
        "node": true,
        "commonjs": true,
        "browser": true,
        "es6": true
       }
Gaither answered 10/4, 2019 at 4:59 Comment(0)
D
62

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

Dipper answered 26/6, 2019 at 16:9 Comment(2)
This is useful if you're using a bundler like Parcel, which exposes process in a browser context.Walkin
This is the correct way if your're implementing a frontend application.Carbolated
P
35

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

Photomicroscope answered 28/3, 2021 at 6:21 Comment(0)
D
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"],
  }
}
Dorothydorp answered 22/6, 2020 at 21:6 Comment(1)
I just had to add "parser": "babel-eslint" to my .eslintrc.json fileBinaural
O
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": {
    }
};
Offal answered 17/4, 2020 at 3:46 Comment(0)
Y
3

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
Yuriyuria answered 3/3, 2022 at 18:28 Comment(0)
R
1

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": {
    }
}
Ruscher answered 17/8, 2022 at 17:25 Comment(0)
N
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.node,
      }
    }
  }
];
Nedi answered 22/4, 2023 at 18:3 Comment(0)
P
0

For a single file, when most other scripts are meant to run in a browser environment, you can add a special comment to the top of a file:

/* eslint-env node */

Note that it has to be a block comment style; an inline style comment (//) will not work.

Pejsach answered 7/2 at 23:50 Comment(0)
I
0

Seems you need to enable Node Core library Within WebStorm:

  1. Go to File -> Settings -> Languages & Frameworks -> Node.js and NPM
  2. Make sure that 'Node.js Core library is enabled' by pressing Enable button.
  3. Click OK for saving settings.
Intromit answered 16/2 at 12:37 Comment(0)
L
-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.

Libelous answered 19/1, 2021 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.