refactor(create): consolidate @lerna/create into the main lerna package#4300
Conversation
The create command was the only command not bundled into the main lerna package due to its use of require.resolve() with __dirname to locate lerna-module-data.js at runtime (consumed by init-package-json). This is solved by configuring esbuild to copy lerna-module-data.js as an asset into dist/commands/create/lib/, matching the path the bundled code expects. The create command is now imported and bundled like every other command. - Bundle create command via TypeScript import instead of external require - Copy lerna-module-data.js as esbuild asset for runtime resolution - Move unit tests and fixtures from legacy-structure into libs/commands/create - Remove packages/legacy-structure/commands/create entirely - Remove @lerna/create dependency, path mapping, and esbuild external
The legacy-structure directory only contained orphaned mock files that are unused — all tests use the equivalent mocks from libs/test-helpers. Also clean up stale references in workspaces, CI workflow, gitignore, and release scripts.
|
View your CI Pipeline Execution ↗ for commit 22d17ef
☁️ Nx Cloud last updated this comment at |
When bundled into the main dist/index.js, __dirname is dist/ so the asset needs to be at dist/lib/lerna-module-data.js. When loaded via the separate entry point at dist/commands/create/index.js, __dirname is dist/commands/create/ so it also needs to be there. Copy to both locations to handle either entry path.
There was a problem hiding this comment.
Important
At least one additional CI pipeline execution has run since the conclusion below was written and it may no longer be applicable.
Nx Cloud is proposing a fix for your failed CI:
We added the missing output: "lib" asset copy entry to packages/lerna/project.json to fix all failing E2E tests. When esbuild bundles the create command into dist/index.js, __dirname resolves to dist/, causing require.resolve(path.join(__dirname, "./lib/lerna-module-data.js")) to look for dist/lib/lerna-module-data.js — a path that only existed in the PR's intent but was absent from the final merged file. This restores the file to both expected locations so the create command works whether invoked via the main CLI bundle or its own entry point.
Warning
The suggested diff is too large to display here, but you can view it on Nx Cloud ↗
Because this branch comes from a fork, it is not possible for us to apply fixes directly, but you can apply the changes locally using the available options below.
Apply changes locally with:
npx nx-cloud apply-locally ZV5F-jNUq
Apply fix locally with your editor ↗ View interactive diff ↗
🎓 Learn more about Self-Healing CI on nx.dev
Note
🤖 This PR was created by @AI-JamesHenry, an AI assistant account guided and overseen by @JamesHenry.
Summary
createcommand into the mainlernapackage like every other command, eliminating the need for@lerna/createto exist as a separate published packageLERNA_MODULE_DATAbundling issue by configuring esbuild to copylerna-module-data.jsas an asset into the dist output, soinit-package-jsoncan still resolve it at runtimelibs/commands/create/where they belongpackages/legacy-structure/entirely — the only remaining contents were orphaned mock files unused by any testWhat changed
The
createcommand was the only command not bundled into the main lerna package. This was due to its use ofrequire.resolve()with__dirnameto locatelerna-module-data.jsat runtime (consumed byinit-package-json). When esbuild bundles the code,__dirnamepoints to the output directory — so the file needs to exist there too.The fix: configure esbuild to copy
lerna-module-data.jsas an asset todist/commands/create/lib/, matching where the bundled code expects it. The create command is now imported via TypeScript and bundled like every other command.Files changed
packages/lerna/src/index.ts—importinstead ofrequire("@lerna/create/command")packages/lerna/project.json— asset copy config, entry points, removed externalpackages/lerna/package.json— removed@lerna/createdependencytsconfig.base.json— removed@lerna/createpath mappinglibs/commands/create/— added jest config, test target, moved tests + fixtures herepackages/legacy-structure/— deleted entirelypackage.json— removed dead workspace entry.github/workflows/pkg-pr-new.yml— removed legacy-structure publish pathTest plan
nx run-many -t buildpassesnx run-many -t testpasses (19 projects, including create command tests)nx run-many -t lintpasses (34 projects)npm run format:checkpassesnx e2e e2e-createpasses (60 tests)nx sync— workspace up to date