import/no-internal-modules
Disallow importing a file from inside another directory or package
Also called import-x/no-internal-modules
A directory with an index.js works like a package. It decides what the outside gets to see. An import that reaches past it, such as ./api/users or lodash/get, depends on how the inside is laid out, so it breaks when someone moves or renames a file in there.
An import path can have one step, like ./api, ../api, lodash or @scope/package. A longer path is reported if the file exists. The rule also checks export ... from, import() and require().
Configure#
Off by default. To turn it on:
[lint.rules]
"import/no-internal-modules" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"import/no-internal-modules" = ["error", {
allow = [], # list of strings
# forbid = <list of strings>,
}]Examples#
Reported:
export { listUsers } from "./users";export const listUsers = () => fetch("/users");import { listUsers } from "./api/users"Reaching to ./api/users is not allowed.;Not reported:
export { listUsers } from "./users";export const listUsers = () => fetch("/users");import { listUsers } from "./api";Options#
| Option | Default | What it does |
|---|---|---|
allow | [] | Globs. A longer path is allowed if a glob matches the import path (without its ./ or ../) or the file's full path. |
forbid | none | Globs, matched the same way. Only paths that match a glob are reported, and every other path is allowed. |
Use allow or forbid, not both.
With { "allow": ["api/*"] }:
export const listUsers = () => fetch("/users");{ "allow": ["api/*"] }No problemsimport { listUsers } from "./api/users";With { "forbid": ["db/*"] }, only the second import is reported:
export const listUsers = () => fetch("/users");export const pool = [];{ "forbid": ["db/*"] }1 problemimport { listUsers } from "./api/users";
import { pool } from "./db/pool"Reaching to ./db/pool is not allowed.;