Can the #billing module import #auth without creating a cycle? With layers you can see the answer, and ESLint enforces it.
This plugin turns the order of your package.json imports into those layers, and reports upward imports and deep imports into another module's internals.
Works in both ESLint and Oxlint.
A cycle compiles fine, then bites at runtime: ReferenceError: Cannot access 'x' before initialization in native ESM, or a silent undefined from CommonJS output and some bundlers.
Worse, it hides until something shuffles the import order.
Here, each # alias is a module with one front door, and the alias order is a one-way layer order.
A module imports only the modules listed below it, so cycles between modules can't form in the first place.
Unlike cycle detectors that walk the whole import graph, each rule checks one file against the declared layers, so it stays fast and the error names the alias to fix.
package.json becomes your architecture map.
npm install --save-dev eslint-plugin-import-structure
# for ESLint, also:
npm install --save-dev eslint typescript-eslint jitiRequires Node.js 22.18+.
// ESLint 9+: eslint.config.ts (needs eslint, typescript-eslint, and jiti)
import plugin from "eslint-plugin-import-structure";
import { parser } from "typescript-eslint";
const ts = { files: ["**/*.ts"], languageOptions: { parser } };
export default [ts, plugin.configs.modules];// Oxlint, through Vite+: vite.config.ts
import plugin from "eslint-plugin-import-structure";
import { defineConfig } from "vite-plus";
const rules = { ...plugin.configs.modules.rules };
export default defineConfig({
lint: { jsPlugins: ["eslint-plugin-import-structure"], rules },
});Then declare your modules in package.json, top layer first:
{
"imports": {
"#billing": "./src/billing/index.ts",
"#auth": "./src/auth/index.ts",
"#db": "./src/db/index.ts"
}
}// in src/auth/session.ts
import { charge } from "#billing"; // ❌ upward import (enforce-layer-order)
import { query } from "#db"; // ✅ a lower layer, through its alias
import { pool } from "#db/pool.ts"; // ❌ deep import (no-barrel-bypass)
import { x } from "../x.ts"; // ❌ parent path (no-parent-import)
import { hash } from "./hash.ts"; // ✅ a sibling in the same modulemodules: every rule below, all aterror.all: every rule, including rules added later. Today it equalsmodules.
No rule takes options.
The order of the # aliases in package.json is the layer order, top first; a module may only import aliases declared below its own.
An alias owns its target, and code outside the owned area reaches it only through the alias.
The rules never depend on folder or file names; an alias may point anywhere.
- An alias to a folder's
indexfile (index.ts, or any other extension) owns that folder and everything below it. An alias to any other file owns only that file. - An alias may sit inside another alias's area. The deeper alias owns its part, carved out of the outer area, and the outer alias owns the rest; the two are ordered like any other pair of aliases.
- No 2 aliases may have the same area, such as the same target or 2
indexfiles in one folder.enforce-layer-orderreports an overlap once at the top of every file both own, since lint cannot report onpackage.json. - Outside an area, any other route to an owned file is a bypass: a deeper alias specifier such as
#a/one.ts(no-barrel-bypass), or a relative path (no-relative-bypass, andprefer-alias-import, which rewrites a path to the alias's target, in any spelling TypeScript resolves to it). - Inside an area, siblings are free, and a subfolder is reached only through its direct
index.ts, like a Rustmod.rs(no-relative-bypass); a path deeper in the subfolder, a grandchild'sindex.tsincluded, fires, and a subfolder without anindex.tscannot be imported at all. ../is banned in every file, owned or not (no-parent-import). Anything above a file needs an alias.- Files no alias owns need no
index.tsand no alias, and import./siblings and subfolders freely. - Don't declare
/*subpath patterns: any use of one that reaches an owned area is a bypass (no-barrel-bypass). Entry points are namedindex.tsonly (enforce-mod-file).
// in main.ts, which no alias owns
import { a } from "#a"; // ✅
import { two } from "./a/two.ts"; // ❌ relative path into #a
import { f } from "./b/f.ts"; // ❌ relative path to a file alias; use "#f"
import { g } from "./b/g.ts"; // ✅ unowned
// in a/one.ts, owned by #a
import { two } from "./two.ts"; // ✅ sibling
import { other } from "./other/index.ts"; // ✅ direct subfolder's index.ts
import { deep } from "./other/deep.ts"; // ❌ past ./other/index.ts
import { sub } from "./sub/x.ts"; // ❌ relative path into #a-sub
import { sub } from "#a/sub/x.ts"; // ❌ deeper alias specifier; use "#a-sub"The other rules (import order, duplicate sources, wildcards, absolute paths, self-imports) are not about aliases and apply to every file.
💼 Configurations enabled in.
🧭 Set in the modules configuration.
🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.
| Name | Description | 💼 | 🔧 | 💡 |
|---|---|---|---|---|
| enforce-import-order | Enforce grouped and sorted import declarations and import members | 🧭 | 🔧 | |
| enforce-layer-order | Enforce the order of package.json imports as the layer order |
🧭 | ||
| enforce-mod-file | Enforce index.ts as the name of module entry points |
🧭 | ||
| no-absolute-import | Disallow absolute paths and file: URLs as module specifiers |
🧭 | ||
| no-barrel-bypass | Disallow # specifiers deeper than an alias |
🧭 | 💡 | |
| no-duplicate-import-source | Disallow separate import declarations from the same source | 🧭 | ||
| no-parent-import | Disallow parent-relative module specifiers | 🧭 | 🔧 | |
| no-relative-bypass | Disallow relative specifiers that reach into an alias's area from outside it or past a subfolder's index file | 🧭 | ||
| no-self-import | Disallow a module from importing itself | 🧭 | ||
| no-wildcard-import | Disallow namespace imports | 🧭 | ||
| prefer-alias-import | Require the # alias for a module that package.json declares |
🧭 | 🔧 |
License: MIT.