简体   繁体   中英

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)".

Except for this one file, all the files in my project uses the browser env , which is set in my .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-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.

See Specifying Environments in the eslint documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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