簡體   English   中英

ESLint - 未定義“進程”

[英]ESLint - 'process' is not defined

我將 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);

我正在使用VSCode編輯器。 它會自動為 JS 代碼運行 ESLint。

在 IDE 中,我看到最后一行的錯誤如下 -

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

有什么想法嗎?

感謝 @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
    }
}

當我遇到錯誤時,我有"browser": true而不是"node": true 簡單的錯誤。

將 "node": "true" 添加到現有環境列表也可以完成這項工作

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

將 .eslintrc 文件添加到項目的根目錄(如果您還沒有)並定義要忽略的全局變量

{
    "globals": {
        "process": true
      }
}

確保在項目中使用 process.env,但僅在單個配置文件中使用。 考慮添加no-process-env規則。

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

這個配置幫助了我,也可以幫助其他人

{
  "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 文件中

在處理 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": {
}

};

如果即使在 settng "node": true,請記住安裝@types/node

npm i -D @types/node

yarn add -D @types/node

.eslintrc.js文件中添加這些 env 行。

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

此問題的另一個補救措施是簡單地import process from "process"

盡管這不是絕對必要的,但人們可能會爭辯說這是更好的做法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM