Monorepos and workspaces
In a workspace, jscycles checks two things: loops between files inside each package, and loops between the packages themselves.
A typical layout
Section titled “A typical layout”- package.json
"workspaces": ["packages/*", "apps/*"] - tsconfig.json
- jscycles.yaml optional
Directorypackages/
Directorycore/
- package.json
"name": "@acme/core" Directorysrc/
- …
- package.json
Directoryutils/
- package.json
"name": "@acme/utils" Directorysrc/
- …
- package.json
Directoryapps/
Directoryweb/
- package.json
"name": "@acme/web" Directorysrc/
- …
- package.json
Run jscycles at the root. It reports file cycles in @acme/core,
@acme/utils, @acme/web and the root package, and cycles between those
packages.
How members are found
Section titled “How members are found”jscycles looks upward from the scanned directory for the workspace root and reads its member list from the first of:
pnpm-workspace.yaml(itspackagesglobs)package.jsonworkspaces, as an array or{ "packages": [...] }(npm, yarn, bun)tsconfig.jsonprojectreferences
Each member needs a package.json with a name, and names must be unique; a
duplicate name is an error rather than a guess. The full rules are in
package cycles.
Which files belong to which package
Section titled “Which files belong to which package”Every package.json with a name is a package, including the workspace root
and packages nested inside other packages. Each file belongs to its nearest
package, so a file is analyzed once, in the package that owns it.
node_modules, dist, build, .next and .git directories are skipped.
What makes a package depend on another
Section titled “What makes a package depend on another”A file in @acme/web that imports @acme/core is an edge from @acme/web to
@acme/core, however the import is spelled: by name (including a subpath such
as @acme/core/format), through a tsconfig path alias that points into
packages/core, or by a relative path into its directory
(../../core/src/x).
Dependencies listed in package.json but never imported are not edges.
Importing by name is still the better practice: a relative path into another
package bypasses its exports and breaks once the packages are published.
Packages outside the workspace, or no workspace at all
Section titled “Packages outside the workspace, or no workspace at all”Package cycles do not need a workspace. Every scanned package takes part, including the workspace root, packages that are not listed as members, and repositories with several packages and no workspace configuration. Imports that reach another package’s files, by a relative path or a tsconfig alias, are edges between them.
What the workspace adds is bare names: only a workspace member can be
imported as @acme/core. Without a workspace, import 'core' resolves
through node_modules, so it is not an edge to a local folder named core.
See the files behind an edge
Section titled “See the files behind an edge”jscycles --outer -v✗ 1 inter-package cycle
@acme/core -> @acme/utils -> @acme/core @acme/core -> @acme/utils - /repo/packages/core/src/session.ts (imports @acme/utils/strings) @acme/utils -> @acme/core - /repo/packages/utils/src/strings.ts
Summary: 1 inter-package cycle--outer skips the file cycles, which is also faster on large workspaces.
Checking part of a workspace
Section titled “Checking part of a workspace”--only, --exclude and path arguments narrow the check, and package cycles
are found among the selected packages only:
jscycles --only "@acme/*" # members in the @acme scopejscycles --exclude "@acme/web" # everything except one packagejscycles packages/core # one directoryWith Turborepo or Nx
Section titled “With Turborepo or Nx”jscycles analyzes the whole workspace in one run, so run it once from the root rather than as a per-package task:
{ "scripts": { "check:cycles": "jscycles" }}For Nx, module boundary rules enforce which projects may depend on which; jscycles complements them by finding loops between files inside each project.