在 Visual Studio Code 中的 ESLint 中出现了“module 未定义”和“process 未定义”的错误。

193

我已经在我的机器上安装了eslint并且使用visual studio code 我有一些模块和过程需要导出 当我尝试使用"module"或"process"时,它显示错误信息 之前一切都运行良好。

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

这是我的 .eslintrc.json 文件


{

"env": {
    "browser": true,
    "amd": true

},
"parserOptions": {
    "ecmaVersion": 6
  },
"extends": "eslint:recommended",
"rules": {
    "no-console": "off",
    "indent": [
        "error",
        "tab"
    ],
    "linebreak-style": [
        "error",
        "windows"
    ],
    "quotes": [
        "error",
        "single"
    ],
    "semi": [
        "error",
        "always"
    ]
}

}

我想要移除这个错误


10
我找到了,谢谢。请在.eslintrc.json文件中添加以下内容:"globals": { "angular": false, "module": false, "inject": false, "document": false },"env": { "browser": true, "amd": true, "node": true } - MaTHwoG
谢谢 :) 这对我很有帮助。你应该把这个作为一个答案,并附上一个简单的例子。 - gin93r
6个回答

313

您可能正在尝试在节点环境中运行此代码。

env 部分应该如下所示:

"env": {
    "browser": true,
    "amd": true,
    "node": true
},

51
注意:这需要放在您的 .eslintrc 文件中。 - Devin Clark
23
这个配置中的browseramd部分对于解决这个特定错误是不必要的。 - Flimm
2
这应该是正确的答案。 - user13641108
即使我在设置过程中选择了“Node”,它仍会创建“browser”条目。奇怪。 - Florian Walther

153

在你的ESLint配置文件中,只需添加以下内容:

{
  ...
  env: {
    node: true
  }
  ...
}

这样做应该可以修复“module未定义”和“process未定义”的错误。

这假设您正在Node环境中运行。对于浏览器环境,也有“browser”选项。您可以根据需要应用两个选项。

如果您想要防止ESLint检查一些全局变量,那么您需要在配置文件的“globals”部分中添加特定的全局变量。

globals: {
  window: true,
  module: true
}

7
加上 node:true 对我解决了问题。谢谢! - jdromero88

86

11
前端代码的完美解决方案,全局指定显然是错误的。谢谢! - qqilihq
2
这应该是被接受的答案。以最直接和客观的方式消除了问题。 - Adam
2
被接受的答案太宽泛了,你不想让节点语法在任何地方都可以使用,只想在一个文件中使用。 - Kyle
这是一个非常简单的解决方案,并且解释得也很简单。 - asim mehmood
你不能只是在你的 eslintrc.cjs 中添加 overrides 来匹配所有以 *.cjs 结尾的文件并具有 env: { node: true },然后将所有你的 CommonJS 文件重命名为以 .cjs 结尾吗?这样做是否不好呢? - Seangle
如果你有很多文件,那么这是一个有效的选择。但在这种情况下,这是一个更简单的选择:文件本身决定如何处理lint,在前端项目中,你通常只有几个可能以node模块样式编写的配置文件。 - rfreytag

6

我认为你可以将所有的CommonJS配置文件重命名为.cjs作为它们的扩展名,然后将此添加到eslintrc.cjs中:

module.exports = {
  // ...
  env: {
    // If you don't want to change this to `node: true` globally
    es2022: true,
  },
    // then add this:
  overrides: [
    {
      files: ['**/*.cjs'],
      env: {
        node: true,
      },
    },
  ],
}

1
我选择这种方法。 - Bryan Lumbantobing
1
谢谢,这对我在2023年的新项目有帮助。具体来说,我需要使用“overrides”才能使其正常工作。 - Joe Sadoski

0
在我的情况下,以下代码解决了这些问题。

eslintrc.cjs

module.exports = {
  env: { browser: true, es2020: true,es2022: true },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    'plugin:react/jsx-runtime',
    'plugin:react-hooks/recommended',
  ],
  overrides: [
    {
      files: ['**/*.cjs'],
      env: {
        node: true,
      },
    },
  ],
  parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
  settings: { react: { version: '18.2' } },
  plugins: ['react-refresh'],
  rules: {
    'react-refresh/only-export-components': 'warn',
    "react/prop-types": "off"
  },
}


0
在我的 .eslintrc.cjs 文件中。
module.exports = {
  env: { browser: true,es2020: true },
   /*rest of code goes here*/
}

我已经向 env 对象添加了 node 属性,并将其设置为 true node: true,所以最终结果将是:

module.exports = {
  env: { browser: true, node: true, es2020: true },
   /*rest of code goes here*/
}
  

现在没有问题了

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