55

I'm trying to add a background image from react native BackgroundImage which needs to add source as source={require('*--iamge path --*')}

but my eslint throw Require statement not part of import statement. error. i have tried to import image in different ways for example:

 - const image = '--image path--'
 - import image = require('--image path--')
 - <BackgroundImage source={require('--image path--')}>
Nipun Ravisara
  • 3,629
  • 3
  • 20
  • 35

5 Answers5

113

you can disable this check in eslintrc.js file

module.exports = {
  ...
  rules: {
    ...
    '@typescript-eslint/no-var-requires': 0,
  }
}
Tuan Luong
  • 3,902
  • 1
  • 16
  • 18
  • Can you provide the documentation for this? [eslint.org](https://eslint.org/docs/rules/) is totally silent about this as far as I can tell. – samurai_jane Aug 19 '20 at 17:15
  • 1
    You can check it here https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-var-requires.md – Tuan Luong Aug 20 '20 at 02:29
  • @samurai_jane There is no reason eslint.org should say anything about this. Eslint.org tells you the general form on how to enable and disable rules. Like here: `rulename: value`. These rules are from an external package, typescript-eslint, so why would eslint.org say anything about it? Everyone can create their own rules. – oligofren Feb 17 '21 at 13:19
52

It's also possible to disable it for individual files, instead of globally disabling it for the whole project.

Add at the top of the file:

/* eslint @typescript-eslint/no-var-requires: "off" */
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
1

Got the same error with typescript lint, locally disable this rule by adding this line right before the require statement worked for me:

/* tslint:disable no-var-requires */
s-hunter
  • 24,172
  • 16
  • 88
  • 130
1

you can create .eslingignore in your project same level as .eslintrc.js and add directories that you want to ignore.

That happened to me when I was working with react project with aws-amplify to communicate with aws. So I could either create another .eslintrc.js in amplify directory which has nodejs code. Instead, I created .eslingignore on the root level and added "amplify" directory.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
0

In ESLint config try to add this:

overrides: [
  {
    files: ['path-to-file', ...],
    rules: {
      '@typescript-eslint/no-var-requires': 'off',
    },
  },
],
StasNemy
  • 142
  • 2
  • 4