ESLint - 未定义"过程"

Aru*_*nko 25 javascript node.js eslint visual-studio-code

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

我正在使用VSCode编辑器.它自动运行ESLint for JS代码.

在IDE中,我看到下面的错误,但最后一行 -

[eslint] 'process' is not defined. (no-undef)
Run Code Online (Sandbox Code Playgroud)

任何想法有什么不对?

Aru*_*nko 54

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

我用以下配置修复了这个.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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 就我而言,我的 env eslint 配置文件中缺少“node: true”。 (3认同)

crt*_*tag 12

在现有环境列表中添加“ node”:“ true”也可以完成此工作

"env": {
        "node": true,
        "commonjs": true,
        "browser": true,
        "es6": true
       }
Run Code Online (Sandbox Code Playgroud)


小智 10

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

{
    "globals": {
        "process": true
      }
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果您正在实现前端应用程序,这是正确的方法。 (3认同)

小智 10

如果你安装env: { node: true }了 eslint ,添加到 .eslintrc.js 文件中


小智 8

这个配置帮助了我,也可以帮助其他人

{
  "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"],
  },
}
Run Code Online (Sandbox Code Playgroud)


小智 5

在处理 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": {
}
Run Code Online (Sandbox Code Playgroud)

};