简体   繁体   English

仅在一个文件中使用 ESLint “require is not defined”时如何解决?

[英]How to fix ESLint “require is not defined” when it is used in just one file?

When I use require in my JavaScript, I get a warning from eslint, "require is not defined eslint(no-undef)".当我在 JavaScript 中使用require时,我收到来自 eslint 的警告,“require is not defined eslint(no-undef)”。

Except for this one file, all the files in my project uses the browser env , which is set in my .eslintrc .除了这个文件,我项目中的所有文件都使用浏览器env ,它是在我的.eslintrc中设置的。

How can I make an exception and specify a node env for just this one file?我怎样才能例外并为这个文件指定一个节点环境?

var fs = require('fs');
var path = require('path');

// my code

Similar to how you can override eslint rules with a code comment at the top of the file, you can also override the environment:与在文件顶部使用代码注释覆盖 eslint 规则类似,您也可以覆盖环境:

/* eslint-env node */

var fs = require('fs');
var path = require('path');

// my code

Setting the node environment will make it so that node features like require and __dirname are not treated as undefined.设置节点环境将使诸如require__dirname类的节点功能不会被视为未定义。

See Specifying Environments in the eslint documentation.请参阅 eslint 文档中的指定环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM