|
| 1 | +# node/no-restricted-import |
| 2 | +> disallow specified modules when loaded by `require` |
| 3 | +
|
| 4 | +## 📖 Rule Details |
| 5 | + |
| 6 | +This rule allows you to specify modules that you don’t want to use in your application. |
| 7 | + |
| 8 | +### Options |
| 9 | + |
| 10 | +The rule takes an array as options: the names of restricted modules. |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "no-restricted-import": ["error", [ |
| 15 | + "foo-module", |
| 16 | + "bar-module" |
| 17 | + ]] |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +You may also specify a custom message for each module you want to restrict as follows: |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "no-restricted-import": ["error", [ |
| 26 | + { |
| 27 | + "name": "foo-module", |
| 28 | + "message": "Please use foo-module2 instead." |
| 29 | + }, |
| 30 | + { |
| 31 | + "name": "bar-module", |
| 32 | + "message": "Please use bar-module2 instead." |
| 33 | + } |
| 34 | + ]] |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +And you can use glob patterns in the `name` property. |
| 39 | + |
| 40 | +```json |
| 41 | +{ |
| 42 | + "no-restricted-import": ["error", [ |
| 43 | + { |
| 44 | + "name": "lodash/*", |
| 45 | + "message": "Please use xyz-module instead." |
| 46 | + }, |
| 47 | + { |
| 48 | + "name": ["foo-module/private/*", "bar-module/*", "!baz-module/good"], |
| 49 | + "message": "Please use xyz-module instead." |
| 50 | + } |
| 51 | + ]] |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +And you can use absolute paths in the `name` property. |
| 56 | + |
| 57 | +```js |
| 58 | +module.exports = { |
| 59 | + overrides: [ |
| 60 | + { |
| 61 | + files: "client/**", |
| 62 | + rules: { |
| 63 | + "no-restricted-import": ["error", [ |
| 64 | + { |
| 65 | + name: path.resolve(__dirname, "server/**"), |
| 66 | + message: "Don't use server code from client code." |
| 67 | + } |
| 68 | + ]] |
| 69 | + } |
| 70 | + }, |
| 71 | + { |
| 72 | + files: "server/**", |
| 73 | + rules: { |
| 74 | + "no-restricted-import": ["error", [ |
| 75 | + { |
| 76 | + name: path.resolve(__dirname, "client/**"), |
| 77 | + message: "Don't use client code from server code." |
| 78 | + } |
| 79 | + ]] |
| 80 | + } |
| 81 | + } |
| 82 | + ] |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Examples |
| 87 | + |
| 88 | +Examples of **incorrect** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules: |
| 89 | + |
| 90 | +```js |
| 91 | +/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/ |
| 92 | + |
| 93 | +import fs from 'fs'; |
| 94 | +import cluster from 'cluster'; |
| 95 | +import pick from 'lodash/pick'; |
| 96 | +``` |
| 97 | + |
| 98 | +Examples of **correct** code for this rule with sample `"fs", "cluster", "lodash"` restricted modules: |
| 99 | + |
| 100 | +```js |
| 101 | +/*eslint no-restricted-import: ["error", ["fs", "cluster", "lodash/*"]]*/ |
| 102 | + |
| 103 | +import crypto from 'crypto'; |
| 104 | +import _ from 'lodash'; |
| 105 | +``` |
| 106 | + |
| 107 | +```js |
| 108 | +/*eslint no-restricted-import: ["error", ["fs", "cluster", { "name": ["lodash/*", "!lodash/pick"] }]]*/ |
| 109 | + |
| 110 | +import pick from 'lodash/pick'; |
| 111 | +``` |
| 112 | + |
| 113 | +## 🔎 Implementation |
| 114 | + |
| 115 | +- [Rule source](../../lib/rules/no-restricted-import.js) |
| 116 | +- [Test source](../../tests/lib/rules/no-restricted-import.js) |
0 commit comments