Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 719
handlebears Avatar asked Oct 14 '25 15:10

handlebears


1 Answers

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.

like image 184
handlebears Avatar answered Oct 17 '25 08:10

handlebears



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!