When you run ESLint through a typescript file, you may run into this warning.
Require statement not part of import statement.eslint[@typescript-eslint/no-var-requires](https://typescript-eslint.io/rules/no-var-requires)
History
JavaScript from ES2015 onwards uses imports as part of the language specification. TypeScript will compile them into environment specific forms as needed. The reason for this is because require([]) and require(””) are environment specific and are more difficult to statically analyze.
Severity
This is for the most part only a syntactical rule. If you don't care about TypeScript module syntax, then you will not need this rule.
Solution 1: Remove the variable
❌ Incorrect
var foo = require('foo');
const foo = require('foo');
let foo = require('foo');
✅ Correct
import foo = require('foo');
require('foo');
import foo from 'foo';
Solution 2: Ignore the instance
Solution 2 is a band-aid solution that tells TS to simply ignore the part of the file that is giving the warning.
Place this line right before the require statement:
/* tslint:disable no-var-requires */
Solution 3: Ignore at file level
Ignore all instances of var requires at the file level.
At the top of the file, place one of the below two lines:
/* eslint @typescript-eslint/no-var-requires: "off" */
OR
/* eslint-disable @typescript-eslint/no-var-requires */
Solution 4: Ignore at project level
Ignore every instance of variable requires usage
In your eslintrc.js
file, make your own rule to disable it:
module.exports = {
...
rules: {
...
'@typescript-eslint/no-var-requires': 0,
}
}
Happy ESlinting
Top comments (0)