Ban specific imports with no-restricted-imports
Ban a single import such as lodash in favor of a lighter option, and ban one named export, with the no-restricted-imports rule in ESLint 9.
no-restricted-imports can forbid a whole module, or one name from a module, and print your reason.
export default [
{
rules: {
'no-restricted-imports': [
'error',
{
paths: [
{ name: 'moment', message: 'Use Temporal or date-fns.' },
{
name: 'lodash',
importNames: ['cloneDeep'],
message: 'Use structuredClone.',
},
],
},
],
},
},
];import moment from 'moment';
import { cloneDeep, chunk } from 'lodash';src/dates.js
1:1 error 'moment' import is restricted from being used. Use Temporal or date-fns. no-restricted-imports
2:10 error 'cloneDeep' import from 'lodash' is restricted. Use structuredClone. no-restricted-importspaths matches an exact module name, and importNames narrows it to chosen exports, so chunk stays allowed. This works well for a migration: block the old library for new code, and leave the existing call sites to be fixed over time. See the rule options.
Use patterns for globs. Dynamic import() and require calls are not covered by this rule, so add no-restricted-modules style checks through a plugin if you need them.