close

no-commonjs

Unreleased

Configuration

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

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

Rule Details

Disallow CommonJS imports and exports when migrating to ES modules.

The rule reports require() calls with one string literal or a template literal without substitutions in the module's variable scope. Calls inside functions, dynamic arguments, and calls with multiple arguments are allowed. It also reports module.exports and exports.* member access, including reads.

Examples of incorrect code for this rule:

const path = require('node:path');
module.exports = { path };
exports.name = 'example';

Examples of correct code for this rule:

import path from 'node:path';
export { path };
export const name = 'example';

The rule does not provide automatic fixes or suggestions.

Options

OptionDefaultBehavior
allowRequirefalseAllow all require() calls.
allowConditionalRequiretrueAllow calls inside conditionals or try statements.
allowPrimitiveModulesfalseAllow module.exports assignments without an object literal on the right.

allowRequire

With { "allowRequire": true }, this is allowed:

const path = require('node:path');

CommonJS exports are still reported.

allowConditionalRequire

By default, the following calls are allowed. Set { "allowConditionalRequire": false } to report them:

const optional = enabled && require('optional');

if (enabled) {
  require('optional');
}

try {
  require('optional');
} catch (error) {}

Conditional expressions (condition ? a : b) and logical expressions (&&, ||, ??) also qualify. Loop and switch statements do not.

allowPrimitiveModules

With { "allowPrimitiveModules": true }, these assignments are allowed:

module.exports = 'value';
module.exports = function create() {};

Object literal assignments and exports.* are still reported:

module.exports = { name: 'example' };
exports.create = function create() {};

The legacy string option "allow-primitive-modules" enables the same behavior.

Original Documentation