close

no-dynamic-require

Added in v0.9.4

Configuration

rslint.config.ts
import { defineConfig, importPlugin } from '@rslint/core';

export default defineConfig([
  importPlugin.configs.recommended,
  {
    rules: {
      'import/no-dynamic-require': 'error',
    },
  },
]);

Rule Details

Disallow expressions as the first argument to require(), so tools can identify module dependencies from the source code.

Examples of incorrect code for this rule:

require(name);
require('../' + name);
require(`../${name}`);
require(name());

Examples of correct code for this rule:

require('../name');
require(`../name`);

The rule checks direct calls named require, including locally declared functions with that name. It ignores calls such as require.resolve(name) and module.require(name), calls without arguments, and arguments after the first.

Like upstream, the rule accepts any literal, including numbers, booleans, null, bigints, and regular expressions. It also accepts template literals without substitutions. It does not evaluate expressions, so require('a' + 'b') and require(`${'name'}`) are reported.

The rule does not provide automatic fixes or suggestions.

Options

esmodule

Set { "esmodule": true } to apply the same check to dynamic import() calls. The default is false.

With this option enabled, the following code is incorrect:

import(name);
import(`../${name}`);

The following code is correct with either setting:

import('../name');
import(`../name`);
import name from '../name';

Original Documentation