254

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?

3
  • does ESLINT have some way of informing it you are linting nodejs code? Commented Jun 17, 2018 at 5:37
  • 9
    You probably haven't configured eslint correctly. eslint.org/docs/user-guide/configuring#specifying-environments Commented Jun 17, 2018 at 5:38
  • 2
    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! Commented Jun 17, 2018 at 5:42

19 Answers 19

384

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.

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

6 Comments

This got rid of my ESlint 'URL' is not defined, too.
In my case I was missing the node: true on my env eslint config file.
Adding the "node" : true worked for me also with this issue.
Thought I selected "node" in the wizard at npm init @eslint/config but seems I didn't heh
Note that this isn't how it's done in version 9+. You need to import the "node" globals instead of the "browser" globals.
|
116

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

"env": { "node": true, "commonjs": true, "browser": true, "es6": true } 

Comments

70

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

2 Comments

This is useful if you're using a bundler like Parcel, which exposes process in a browser context.
This is the correct way if your're implementing a frontend application.
49

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, } } } ]; 

2 Comments

This is the one that did it for me! Thank you! if you use --init to create the eslint.config.js file.
For those wondering, you can scope this configuration to just a single file. This is useful in my case where I have frontend and backend code; I scoped it to a central file for verifying environment variables, but needs direct access to the process.env object. See @t3-oss/env-nextjs if you're curious.
36

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

Comments

14

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

1 Comment

I just had to add "parser": "babel-eslint" to my .eslintrc.json file
8

As of version 9 of eslint, the old style of configuration is deprecated. The way it's supposed to be done now is:

import globals from "globals"; import pluginJs from "@eslint/js"; export default [ {languageOptions: { globals: globals.node }}, pluginJs.configs.recommended, ]; 

(obviously, different plugins if you use typescript.)

This is extra annoying, because even if you create the config file with "eslint --init" and say you're running in node, it will by default use globals.browser rather than the correct globals.node.

Comments

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": { } }; 

Comments

4

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 

Comments

4

None of the answers satisfies the valid and ideal working solution currently.

  • First, the file is updated from .eslintrc.js to eslint.config.js or eslint.config.mjs in my case as it's already cited above.
  • For those wondering how to create one, running npm init @eslint/config@latest will result an interactive command line questions to create a config template with defaults.
  • Secondly, this template shipped with an object called languageOptions as stated below.
{languageOptions: { globals: globals.browser }}, 

The problem with that is, this will lead to an error as the enquirer's problem shown with the screenshot below;

Process is not defined error

If you read the previous answers from @Géry Ogam & @Jon Watte, they will lead you to try either one of those alternatives;

{languageOptions: { globals: { ...globals.node }}}, 

or

{languageOptions: { globals: globals.node }}, 

which might result more errors if any of browser object methods used as shown below when running linter even though it fixes the error enquirer ask;

linter errors shown another linter error shown

TL;DR

What to do to surpass all of those unwanted linter errors to let us making use of both cases?

Adding both globals.node and globals.browser into the globals property under languageOptions will resolve the issue and produce no more errors regarding the use cases for both environments.

{languageOptions: { globals: { ...globals.node, ...globals.browser }}}, 

Comments

3

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.

1 Comment

This is the winner!!!
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": { } } 

Comments

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.

Comments

0

Elements of the above solutions are helpful, and also show the answers over time. The main thing I saw was at v9+, there is a new file system for configurations.

Several of the solutions above reference them, but here's what I discovered, and document for anyone else who comes across this:

config files:

.eslintrc.js .eslintrc.json eslint.config.js eslint.config.mjs 

with the v9+, you'll get one of the last 2 of these files based on the answers you provide in the command line initialization (also referenced in other answers above)

Things to note: the first 2 files are now legacy and are useless, so remove or rename them.

The config file that worked, and removed the error (originally stated in this question) was the eslint.config.js/mjs file, and here is my working entry (edited from script auto-generated):

import globals from "globals"; import pluginJs from "@eslint/js"; /** @type {import('eslint').Linter.Config[]} */ export default [ { languageOptions: { globals: { ...globals.node } } }, pluginJs.configs.recommended, ]; 

last notes: This answer solves the exact "process" question originally asked, and will make the lint error disappear. Others have suggested adding the globals.browser entry and have both. This is needed only if you're building FE code/need browser, but strictly, isn't required to answer this question. Add it if needed. (I'm a minimalist in enabling kinda person)

Hope this helps someone :)

Comments

0

Changing it to languageOptions: { globals: globals.node} worked for me.

1 Comment

Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review
0

I had a same problem after I created my react project via VITE.

I solved it like this:

in .env file :
(You should create your .env file inside your root path) Your enviromental variables should be started with VITE_

VITE_BACKEND_URL=http://localhost:5000/api VITE_ASSET_URL=http://localhost:5000 VITE_STH=SOMETHING 

Then inside your codes you can access to your variables as below:

// `${import.meta.env.<name of your variable>}` `${import.meta.env.VITE_BACKEND_URL}/places/${props.id}` 

Comments

0

Extending the answers already provided, if you're working on a browser-based app, you really don't want to allow the Node API in your browser files, so you should restrict your eslint config to support node only in config files. e.g.

{ files: ['**/*.config.js'], languageOptions: { globals: globals.node, }, }, 

You can add any additional files that are run by node and not in the browser to the files array.

Comments

0
import globals from 'globals'; export default [ { languageOptions: { globals: { ...globals.node, }, ecmaVersion: 12, }, rules: { // Your specific rules here } } ]; 

Just add this

...globals.node, 

as eslint not recorginse node global variable

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-4

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.

2 Comments

Down votes with no comments - wow!
@PhillipJacobs And on a working solution :/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.