Search code examples
reactjstypescriptaccessibility

How to resolve @typescript-eslint/no-var-requires error. Error while adding @axe-core/react


I have added axe-core/react into my project by:

npm install --save-dev @axe-core/react

Now I have added the following piece of code in my index.tsx to get it up and running:

if (process.env.NODE_ENV !== 'production') {
    const axe = require('@axe-core/react');
    axe(React, ReactDOM, 1000);
}

Now if I navigate to localhost, the page shows a compile error with the following message:

src\index.tsx
Line 14:17: Require statement not part of import statement
@typescript-eslint/no-var-requires

This error is happening for line:

const axe = require('@axe-core/react');

How do I import it otherwise?

PS: I tried doing something like:

import foo = require("foo")

But still no luck.


Solution

  • Replacing the line

    const axe = require('@axe-core/react');
    

    With

    import axe from '@axe-core/react';
    

    resolved my issue.