# global-require

Rule `node/global-require`. Require `require()` calls to be placed at top-level module scope

- Source: node
- Technologies: Node.js
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 2.1M weekly downloads of presets that enable it
- Trend: +151.7% last quarter
- Rank: #1509 by reach, #13 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `n/global-require` | no | no | package `eslint-plugin-n` | [docs](https://github.com/eslint-community/eslint-plugin-n/blob/HEAD/docs/rules/global-require.md) |
| Oxlint | `node/global-require` | no | no | plugin `node`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/node/global-require.html) |
| Biome | `nursery/useTopLevelRequire` | no | no | group nursery, since 2.6.8 | [docs](https://biomejs.dev/linter/rules/use-top-level-require/) |

## Code examples

### Incorrect: the rule reports this

```js
// calling require() inside of a function is not allowed
function readFile(filename, callback) {
    var fs = require('fs');
    fs.readFile(filename, callback)
}

// conditional requires like this are also not allowed
if (DEBUG) { require('debug'); }

// a require() in a switch statement is also flagged
switch(x) { case '1': require('1'); break; }

// you may not require() inside an arrow function body
var getModule = (name) => require(name);

// you may not require() inside of a function body as well
function getModule(name) { return require(name); }

// you may not require() inside of a try/catch block
try {
    require(unsafeModule);
} catch(e) {
    console.log(e);
}
```

### Correct: the rule accepts this

```js
// all these variations of require() are ok
require('x');
var y = require('y');
var z;
z = require('z').initialize();

// requiring a module and using it in a function is ok
var fs = require('fs');
function readFile(filename, callback) {
    fs.readFile(filename, callback)
}

// you can use a ternary to determine which module to require
var logger = DEBUG ? require('dev-logger') : require('logger');

// if you want you can require() at the end of your module
function doSomethingA() {}
function doSomethingB() {}
var x = require("x"),
    z = require("z");
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import n from "eslint-plugin-n";

export default [
  {
    plugins: { n },
    rules: {
      "n/global-require": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "plugins": [
    "node"
  ],
  "rules": {
    "node/global-require": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useTopLevelRequire": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |

## Related rules

- [eslint/global-require](https://lintradar.dev/rules/eslint/global-require.md): Require `require()` calls to be placed at top-level module scope
- [node/callback-return](https://lintradar.dev/rules/node/callback-return.md): Require `return` statements after callbacks
- [node/exports-style](https://lintradar.dev/rules/node/exports-style.md): Enforce either `module.exports` or `exports`
- [node/no-mixed-requires](https://lintradar.dev/rules/node/no-mixed-requires.md): Disallow `require` calls to be mixed with regular variable declarations
- [node/no-callback-literal](https://lintradar.dev/rules/node/no-callback-literal.md): Enforce Node.js-style error-first callback pattern is followed
- [node/file-extension-in-import](https://lintradar.dev/rules/node/file-extension-in-import.md): Enforce the style of file extensions in `import` declarations

---

Web page: https://lintradar.dev/rules/node/global-require. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
