MaT*_*woG 27 module process eslint
我已经在我的机器中安装了eslint并且我使用了visual studio代码我有一些模块和进程要导出当我尝试使用"模块"或"进程"时它显示它之前工作正常.
[eslint] 'module' is not defined. (no-undef)
[eslint] 'process' is not defined. (no-undef)
Run Code Online (Sandbox Code Playgroud)
这是我的.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"
]
}
Run Code Online (Sandbox Code Playgroud)
}
我想删除此错误
Mih*_*anu 51
您可能正在尝试在节点环境中运行它.
该env部分应如下所示:
"env": {
"browser": true,
"amd": true,
"node": true
},
Run Code Online (Sandbox Code Playgroud)
Joh*_*edy 48
在您的 ESLint 配置文件中,只需添加以下内容:
{
...
env: {
node: true
}
...
}
Run Code Online (Sandbox Code Playgroud)
那应该修复"module" is not defined和"process" is not defined错误。
这假设您在 Node 环境中运行。还有
browser浏览器环境的选项。您可以根据需要同时申请。
如果你想阻止 ESLint 对某些全局变量进行 linting,那么你需要globals在 config 部分添加特定的全局变量。
globals: {
window: true,
module: true
}
Run Code Online (Sandbox Code Playgroud)
Fli*_*imm 30
你需要告诉 eslint 你在 Node 环境中。我最喜欢对一次性文件执行此操作的方法gulpfile.js是在顶部包含此注释:
/* eslint-env node */
Run Code Online (Sandbox Code Playgroud)
您只需将所有 CommonJS 文件重命名为.cjs扩展名,然后将其添加node: true到overrides文件.cjs中即可eslintrc.cjs:
module.exports = {
// ...
env: {
es2023: true,
// If you don't want to add `node: true` globally...
},
// ...then add `overrides`:
overrides: [
{
files: ['**/*.cjs'],
env: {
es2023: true,
node: true,
},
},
],
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我认为必须强调了解运行时环境是什么以及如何配置捆绑器的重要性。为此,我建议阅读:
理解这些概念将防止许多潜在的错误,而且额外的好处是,您可以根据需要灵活地配置 ESLint,而无需搜索特定的现成配置。