This rule enforces (and autofixes) that import
statements and require
calls — henceforth referred to as just "imports" — appear in the order described in our guidelines.
Imports appear in the following order:
- "External" imports (ie. NodeJS built-in modules and dependencies specified in "package.json"; these do not start with "." or "..").
- "Internal" imports (ie. local to the projects, starting with "." or "..");
Within each group, we sort lexicographically (and case-sensitively) by the module source (ie. the thing on the right-hand side).
Between groups, we expect a blank line, but that is enforced separately, by the group-imports rule.
Likewise, we expect imports to appear at the top of the file, before other statements, but that too is enforced separately, by the imports-first rule.
See the links in the Further Reading section below for the rationale behind this pattern.
NOTE: Imports like those in the following example are made for side-effects only and cannot be reordered because we don't know statically what may depend on those side-effects occurring in a particular order.
// A side-effect-only import:
imports 'thing';
// A side-effect-only require:
require('other');
Any time this rule encounters such an import, it considers it a boundary and will not reorder any imports across that boundary.
Examples of incorrect code for this rule:
import {g, z} from 'one';
import x from './x';
import {a} from 'other';
Examples of correct code for this rule:
import {g, z} from 'one';
import {a} from 'other';
import x from './x';