no-missing-require
UnreleasedConfiguration
Disallow require() and require.resolve() calls whose targets cannot be found.
The rule checks constant module arguments, including calls through aliases,
destructured methods, optional calls, and Node's global object. Local bindings
that shadow require are ignored. It reports the argument without offering
fixes or suggestions.
Examples
Incorrect when the targets do not exist:
Correct when existing.js and installed-package exist:
CommonJS directory entries, package main fields, and exports maps are
checked. The default export conditions are node and require. Declaring a
dependency in package.json does not make an uninstalled package available.
Node builtins are accepted; data:, HTTP, and HTTPS URLs still require a
resolvable target.
Constant expressions such as require('pack' + 'age') and template literals
are checked. Identifier arguments such as require(packageName) are ignored,
even when the identifier has a constant initializer. Imports and TypeScript
import value = require('package') declarations are outside this rule.
Options
allowModules: package roots accepted without resolution. Allowingelectron,@scope/package, orvirtual:modulealso allows their subdirectories.resolvePaths: additional base directories searched before the requiring file's directory. Relative entries use the working directory, orsettings.cwdwhen configured.tryExtensions: extensions tried for extensionless targets. The JavaScript defaults are shown above. An empty list disables extension guessing; explicit filenames can still resolve.resolverConfig: overrides module resolution. Supported properties aremodules,alias,fallback,fullySpecified,extensions,extensionAlias,conditionNames,mainFields,mainFiles, andaliasFields. For example,{ modules: ['custom_modules', 'node_modules'] }adds a module directory, and{ alias: { virtual: './shim.js' } }redirects a request. Directory defaults aremainFields: ['main']andmainFiles: ['index'].typescriptExtensionMap: overrides TypeScript extension substitution with[sourceExtension, emittedExtension]pairs, orpreserve,react,react-jsx,react-jsxdev, orreact-native. An empty array disables substitution.tsconfigPath: selects the TypeScript config used for extension mapping. Relative paths use the working directory. An explicit extension map takes precedence. Path aliases still come from the nearesttsconfig.jsonand must resolve to existing targets.
Options can also be supplied through settings.node. Rule options take
precedence, including explicit empty lists.
For TypeScript files, the default mapping substitutes .ts for .js, .mts
for .mjs, .cts for .cjs, and .tsx for .jsx in preserve mode or .js
in React modes. allowImportingTsExtensions uses source extensions directly.
resolverConfig.fallback redirects unresolved requests, for example
{ fallback: { virtual: './shim.js' } }. Existing targets take precedence.
fullySpecified: true disables extension and directory-entry guessing for
ordinary requests: ./helper.js can resolve while ./helper cannot. Package
entry points and alias redirects retain their normal lookup behavior.
Differences from upstream
resolverConfigoptions not listed above, includingsymlinks, are not supported.- Overlapping object-form
aliasandfallbackentries use alphabetical priority instead of declaration order. Use arrays to set priority. - Error messages for malformed
package.jsonfiles, invalid imports mappings, and circular aliases may differ.