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:

bunfig.toml
[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.

bunfig.toml
[lint.rules]
"import/no-internal-modules" = ["error", {
  allow = [], # list of strings
  # forbid = <list of strings>,
}]

Examples#

Reported:

api/index.js
export { listUsers } from "./users";
api/users.js
export const listUsers = () => fetch("/users");
example.js1 problem
import { listUsers } from "./api/users";

Not reported:

api/index.js
export { listUsers } from "./users";
api/users.js
export const listUsers = () => fetch("/users");
example.jsNo problems
import { listUsers } from "./api";

Options#

OptionDefaultWhat 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.
forbidnoneGlobs, 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/*"] }:

api/users.js
export const listUsers = () => fetch("/users");
example.jswith { "allow": ["api/*"] }No problems
import { listUsers } from "./api/users";

With { "forbid": ["db/*"] }, only the second import is reported:

api/users.js
export const listUsers = () => fetch("/users");
db/pool.js
export const pool = [];
example.jswith { "forbid": ["db/*"] }1 problem
import { listUsers } from "./api/users";
import { pool } from "./db/pool";