no-var-requires
Disallow
require
statements except in import statements.
✅
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
In other words, the use of forms such as var foo = require("foo")
are banned. Instead use ES6 style imports or import foo = require("foo")
imports.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-var-requires": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
var foo = require('foo');
const foo = require('foo');
let foo = require('foo');
Open in Playgroundimport foo = require('foo');
require('foo');
import foo from 'foo';
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
If your project frequently uses older CommonJS require
s, then this rule might not be applicable to you.
If only a subset of your project uses require
s then you might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.