javascript ESLint - 'process' 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50894000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:19:11  来源:igfitidea点击:

ESLint - 'process' is not defined

javascriptnode.jsvisual-studio-codeeslint

提问by ArunKolhapur

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

我正在将 ESLinter 用于一个简单的节点项目。下面是我在 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 VSCodeeditor. It automatically runs ESLint for JS code.

我正在使用VSCode编辑器。它会自动为 JS 代码运行 ESLint。

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

在 IDE 中,我在最后一行看到以下错误 -

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

Any Idea what's wrong?

知道出了什么问题吗?

回答by ArunKolhapur

Thanks @FelixKling and @Jaromanda X for quick responses.

感谢 @FelixKling 和 @Jaromanda X 的快速回复。

I have fixed this with following config for .eslintrc.jsonfile-

我已使用以下.eslintrc.json文件配置修复了此问题-

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

When I got error I had "browser": trueinstead of "node": true. Simple mistake.

当我遇到错误时,我有"browser": true而不是"node": true. 简单的错误。

回答by crtag

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

将 "node": "true" 添加到现有的环境列表也可以完成这项工作

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

回答by t-reksio

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

将 .eslintrc 文件添加到项目的根目录(如果您还没有)并定义要忽略的全局变量

{
    "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.

确保在整个项目中使用 process.env,但仅在单个配置文件中使用。考虑添加no-process-env规则。

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

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

回答by Bertwin Romero

when working on node js project. this might help you. it works in my end

在 node js 项目上工作时。这可能对你有帮助。它对我有用

module.exports = {
"env": {
    "node": true,
    "browser": true,
    "commonjs": true,
    "es6": true
},
"extends": "eslint:recommended",
"globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
},
"parserOptions": {
    "ecmaVersion": 2018
},
"rules": {
}

};

};