Enforce a convention in the order of require()
/ import
statements. The order is as shown in the following example:
// 1. node "builtins"
import fs from 'fs';
import path from 'path';
// 2. "external" modules
import _ from 'lodash';
import chalk from 'chalk';
// 3. "internal" modules
// (if you have configured your path or webpack to handle your internal paths differently)
import foo from 'src/foo';
// 4. modules from a "parent" directory
import foo from '../foo';
import qux from '../../foo/qux';
// 5. "sibling" modules from the same or a sibling's directory
import bar from './bar';
import baz from './bar/baz';
// 6. "index" of the current directory
import main from './';
Unassigned imports are ignored, as the order they are imported in may be important.
Statements using the ES6 import
syntax must appear before any require()
statements.
import _ from 'lodash';
import path from 'path'; // `path` import should occur before import of `lodash`
// -----
var _ = require('lodash');
var path = require('path'); // `path` import should occur before import of `lodash`
// -----
var path = require('path');
import foo from './foo'; // `import` statements must be before `require` statement
import path from 'path';
import _ from 'lodash';
// -----
var path = require('path');
var _ = require('lodash');
// -----
// Allowed as ̀`babel-register` is not assigned.
require('babel-register');
var path = require('path');
// -----
// Allowed as `import` must be before `require`
import foo from './foo';
var path = require('path');
This rule supports the following options:
How groups are defined, and the order to respect. groups
must be an array of string
or [string
]. The only allowed string
s are: "builtin"
, "external"
, "internal"
, "parent"
, "sibling"
, "index"
. The enforced order is the same as the order of each element in a group. Omitted types are implicitly grouped together as the last element. Example:
[
'builtin', // Built-in types are first
['sibling', 'parent'], // Then sibling and parent types. They can be mingled together
'index', // Then the index file
// Then the rest: internal and external type
]
The default value is ["builtin", "external", "parent", "sibling", "index"]
.
You can set the options like this:
"import/order": ["error", {"groups": ["index", "sibling", "parent", "internal", "external", "builtin"]}]
Enforces or forbids new lines between import groups:
- If set to
ignore
, no errors related to new lines between import groups will be reported (default). - If set to
always
, at least one new line between each group will be enforced, and new lines inside a group will be forbidden. To prevent multiple lines between imports, coreno-multiple-empty-lines
rule can be used. - If set to
never
, no new lines are allowed in the entire import section.
With the default group setting, the following will be invalid:
/* eslint import/order: ["error", {"newlines-between": "always"}] */
import fs from 'fs';
import path from 'path';
import index from './';
import sibling from './foo';
/* eslint import/order: ["error", {"newlines-between": "never"}] */
import fs from 'fs';
import path from 'path';
import index from './';
import sibling from './foo';
while those will be valid:
/* eslint import/order: ["error", {"newlines-between": "always"}] */
import fs from 'fs';
import path from 'path';
import index from './';
import sibling from './foo';
/* eslint import/order: ["error", {"newlines-between": "never"}] */
import fs from 'fs';
import path from 'path';
import index from './';
import sibling from './foo';
import/external-module-folders
setting