Permalink
Browse files

Fix: Improve the doc for no-restricted-modules rule (fixes #9437) (#9495

)
  • Loading branch information...
vibss2397 authored and gyandeeps committed Oct 27, 2017
1 parent c529de9 commit bb6e60a0eec42e5614a537d9766e8ef42dc806ed
Showing with 43 additions and 23 deletions.
  1. +43 −23 docs/rules/no-restricted-modules.md
@@ -1,29 +1,35 @@
# Disallow Node.js modules (no-restricted-modules)
Disallowing usage of specific Node.js modules can be useful if you want to control the available methods, a developer can
use, to implement a feature.
A module in Node.js is a simple or complex functionality organized in a JavaScript file which can be reused throughout the Node.js
application. The keyword `require` is used in Node.js/CommonJS to import modules into an application. This way you can have dynamic loading where the loaded module name isn't predefined /static, or where you conditionally load a module only if it's "truly required".
This way you can block usage of the `fs` module if you want to disallow file system access.
Blocking the `os` module can be useful if you don't want to allow any operating system specific code.
Why would you want to restrict a module?
Disallowing usage of specific Node.js modules can be useful if you want to limit the available methods a developer can use. For example,You can block usage of the `fs` module if you want to disallow file system access.
## Rule Details
This rule allows you to specify modules that you don't want to use in your application.
This rule allows you to specify modules that you dont want to use in your application.
## Options
The rule takes one or more strings as options: the names of restricted modules.
It can also take an object with lists of "paths" and gitignore-style "patterns" strings.
```json
"no-restricted-modules": ["error", "foo-module", "bar-module"]
```
For example, to restrict the use of all Node.js core modules (via https://github.com/nodejs/node/tree/master/lib):
It can also take an object with lists of `paths` and gitignore-style `patterns` strings.
```json
{
"no-restricted-modules": ["error",
"assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib"
]
}
"no-restricted-modules": ["error", { "paths": ["foo-module", "bar-module"] }]
```
```json
"no-restricted-modules": ["error", {
"paths": ["foo-module", "bar-module"],
"patterns": ["foo-module/private/*", "bar-module/*","!baz-module/good"]
}]
```
You may also specify a custom message for any paths you want to restrict as follows:
@@ -32,23 +38,37 @@ You may also specify a custom message for any paths you want to restrict as foll
"no-restricted-modules": ["error", [{
"name": "foo-module",
"message": "Please use bar-module instead."
}]]
}]
]
```
or like this:
```json
"no-restricted-modules": ["error", {
"paths": [{
"name": "foo-module",
"message": "Please use bar-module instead."
"no-restricted-modules": ["error",{
"paths":[{
"name": "foo-module",
"message": "Please use bar-module instead."
}]
}]
```
The custom message will be appended to the default error message. Please note that you may not specify custom error messages for restricted patterns as a particular module may match more than one pattern.
Examples of **incorrect** code for this rule with sample `"fs", "cluster"` restricted modules:
To restrict the use of all Node.js core modules (via https://github.com/nodejs/node/tree/master/lib):
```json
{
"no-restricted-modules": ["error",
"assert","buffer","child_process","cluster","crypto","dgram","dns","domain","events","freelist","fs","http","https","module","net","os","path","punycode","querystring","readline","repl","smalloc","stream","string_decoder","sys","timers","tls","tracing","tty","url","util","vm","zlib"
]
}
```
## Examples
Examples of **incorrect** code for this rule with sample `"fs", "cluster","loadash"` restricted modules:
```js
/*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
@@ -58,18 +78,18 @@ var cluster = require('cluster');
```
```js
/*eslint no-restricted-modules: ["error", { "paths": ["cluster"] }]*/
/*eslint no-restricted-modules: ["error", {"paths": ["cluster"] }]*/
var cluster = require('cluster');
```
```js
/*eslint no-restricted-modules: ["error", { "patterns": ["lodash/*"] }]*/
var cluster = require('lodash/pick');
var pick = require('lodash/pick');
```
Examples of **correct** code for this rule with sample `"fs", "cluster"` restricted modules:
Examples of **correct** code for this rule with sample `"fs", "cluster","loadash"` restricted modules:
```js
/*eslint no-restricted-modules: ["error", "fs", "cluster"]*/
@@ -84,5 +104,5 @@ var crypto = require('crypto');
}]*/
var crypto = require('crypto');
var eslint = require('lodash/pick');
```
var pick = require('lodash/pick');
```

0 comments on commit bb6e60a

Please sign in to comment.