在 ESLint 中,“require”和“process”未定义。是与 Node.js 有关的问题吗?

49

我在GitLab的流水线中遇到了一个错误。我根据StackOverflow上的信息更改了.eslint.json的设置,但仍然存在问题。

我的.eslint.json看起来像这样:

我的.eslint.json文件如下:

{
  "extends": "eslint:recommended",
  "rules": {
    "semi": ["warn", "never"],
    "quotes": ["warn", "single"],
    "no-console": ["off"]
  },
  "parserOptions": {
    "ecmaVersion": 9
  },
  "env": {
    "es6": true,
    "node": true,
    "browser": true,
    "amd": true
  },
  "globals": {
    "$": true,
    "require": true
    "process": true
  },
  "root": true
}

env中,我添加了"adm": true,并在globals中添加了"process": true"require": true

错误如下:

错误:'require'未定义 no-undef

错误:'process'未定义 no-undef

包含错误的文件如下所示:

const qs = require("querystring");

const coEndpoint =
    process.env.NODE_ENV == "production"

那么问题出在哪里?是环境节点的问题吗?我应该如何修复它呢?


4
向全局变量中添加 "require": true - MjZac
是的,但我也在全局变量中添加了process:true,仍然存在错误。 - anna
我按照你说的做了,但还是有问题 :/ 怎么回事? - anna
2个回答

180

在配置文件中指定环境,请使用 env 键并通过将每个环境设置为 true 来指定要启用的环境。例如,以下代码启用了浏览器、es6 和 Node.js 环境:

在你的.eslintrc.js文件中;

...
env: {
   browser: true,
   node: true,    <<<<--- Add this
   es6: true
 },
...

4
在安装过程中,它问我是否应该将eslint用于浏览器或node。我选择了node。但正如你所指出的那样,我们似乎还需要填写这个部分。我永远不会发现这一点。非常感谢! - klewis
2
这个答案对我有用。它的文件名不是'.eslintrc.js',而是'.eslintrc'。 - Steve Smith
2
在我的Webpack配置文件中添加node:true解决了我的eslint错误。谢谢! - C-Dev
2
node:true也帮助我解决了我的eslint错误。谢谢! - Andrei

0
.eslint.json 重命名为 .eslintrc.json 或确保在您的 package.json 文件中指定了 eslintConfig

https://eslint.org/docs/user-guide/configuring

同时确保在你的 .eslintrc.json 文件所在的目录中启动 eslint,并且不要使用 --no-eslintrc 选项启动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接