DEV Community

Dave
Dave

Posted on

11 2

Fixing "Require statement not part of import statement.eslint[@typescript-eslint/no-var-requires]"

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)
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode

✅ Correct

import foo = require('foo');
require('foo');
import foo from 'foo';
Enter fullscreen mode Exit fullscreen mode

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 */
Enter fullscreen mode Exit fullscreen mode

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" */
Enter fullscreen mode Exit fullscreen mode

OR

/* eslint-disable @typescript-eslint/no-var-requires */
Enter fullscreen mode Exit fullscreen mode

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,
  }
}
Enter fullscreen mode Exit fullscreen mode

Happy ESlinting

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay