Skip to content

Monorepos and workspaces

In a workspace, jscycles checks two things: loops between files inside each package, and loops between the packages themselves.

  • package.json "workspaces": ["packages/*", "apps/*"]
  • tsconfig.json
  • jscycles.yaml optional
  • Directorypackages/
    • Directorycore/
      • package.json "name": "@acme/core"
      • Directorysrc/
        • …
    • Directoryutils/
      • package.json "name": "@acme/utils"
      • Directorysrc/
        • …
  • Directoryapps/
    • Directoryweb/
      • package.json "name": "@acme/web"
      • Directorysrc/
        • …

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.

jscycles looks upward from the scanned directory for the workspace root and reads its member list from the first of:

  1. pnpm-workspace.yaml (its packages globs)
  2. package.json workspaces, as an array or { "packages": [...] } (npm, yarn, bun)
  3. tsconfig.json project references

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.

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.

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.

Terminal window
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.

--only, --exclude and path arguments narrow the check, and package cycles are found among the selected packages only:

Terminal window
jscycles --only "@acme/*" # members in the @acme scope
jscycles --exclude "@acme/web" # everything except one package
jscycles packages/core # one directory

jscycles analyzes the whole workspace in one run, so run it once from the root rather than as a per-package task:

package.json
{
"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.