简体   繁体   English

ESLint - 未定义“进程”

[英]ESLint - 'process' is not defined

I am using ESLinter for a simple node project.我将 ESLinter 用于一个简单的节点项目。 Below is the only code I have in index.js:以下是我在 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.我正在使用VSCode编辑器。 It automatically runs ESLint for JS code.它会自动为 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?有什么想法吗?

Thanks @FelixKling and @Jaromanda X for quick responses.感谢 @FelixKling 和 @Jaromanda X 的快速回复。

I have fixed this with following config for .eslintrc.json file-我已经使用以下.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": true instead of "node": true .当我遇到错误时,我有"browser": true而不是"node": true Simple mistake.简单的错误。

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
       }

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.确保在项目中使用 process.env,但仅在单个配置文件中使用。 Consider adding no-process-env rule.考虑添加no-process-env规则。

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

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

如果您安装了 eslint,请将env: { node: true }到 .eslintrc.js 文件中

when working on node js project.在处理 node js 项目时。 this might help you.这可能对你有帮助。 it works in 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": {
}

}; };

If you are getting the error even after settng "node": true, remember to install @types/node with如果即使在 settng "node": true,请记住安装@types/node

npm i -D @types/node

yarn add -D @types/node

Add these env lines in your .eslintrc.js file..eslintrc.js文件中添加这些 env 行。

module.exports = {
    "env": {
        "browser": true,
        "commonjs": true,
        "node": true,
        "es2021": true,
        "jest": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
        "ecmaVersion": "latest"
    },
    "rules": {
    }
}

Another remedy for this issue is to simply import process from "process" .此问题的另一个补救措施是简单地import process from "process"

Even though it's not strictly necessary, one could argue it's better practice anyway.尽管这不是绝对必要的,但人们可能会争辩说这是更好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM