persona-community-5/.pnpm-store/v3/files/06/7f656a3410a52f86d53e085f247d3f75aa6343df5d0094c1cfd408e162b467cb55085213636a829f895bfe0bb1d701e7f9cab24a2d7d3c2837f6bedde8d2e3
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

70 lines
2.1 KiB
Plaintext

---
description: 'Disallow `require` statements except in import statements.'
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/no-var-requires** for documentation.
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.
## Examples
<Tabs>
<TabItem value="❌ Incorrect">
```ts
var foo = require('foo');
const foo = require('foo');
let foo = require('foo');
```
</TabItem>
<TabItem value="✅ Correct">
```ts
import foo = require('foo');
require('foo');
import foo from 'foo';
```
</TabItem>
</Tabs>
## Options
### `allow`
A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work.
With `{allow: ['/package\\.json$']}`:
<Tabs>
<TabItem value="❌ Incorrect">
```ts option='{ "allow": ["/package.json$"] }'
const foo = require('../data.json');
```
</TabItem>
<TabItem value="✅ Correct">
```ts option='{ "allow": ["/package.json$"] }'
const foo = require('../package.json');
```
</TabItem>
</Tabs>
## 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](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.
## Related To
- [`no-require-imports`](./no-require-imports.mdx)