Skip to content

fix: bundle CLI dependencies to resolve global module resolution issues#125

Open
diegomarino wants to merge 1 commit intomainfrom
fix/convert-cli-to-commonjs
Open

fix: bundle CLI dependencies to resolve global module resolution issues#125
diegomarino wants to merge 1 commit intomainfrom
fix/convert-cli-to-commonjs

Conversation

@diegomarino
Copy link
Contributor

Summary

Fixes module resolution issues in global npm installations by bundling CLI dependencies directly in the package.

Problem

Users installing legal-markdown-js globally (especially with Homebrew-installed Node) were experiencing:

TypeError: Cannot read properties of undefined (reading 'action')
Error: Cannot find module 'commander'

This happened because ESM module resolution in global npm installs doesn't always correctly resolve dependencies in the package's node_modules.

Solution

Used bundleDependencies to bundle CLI-specific dependencies (commander, chalk, @inquirer/prompts) directly in the npm tarball. This is the standard npm solution for CLI packages.

Why this works:

  • Dependencies are physically present in the published package's node_modules
  • No reliance on npm's module resolution in global installs
  • Works consistently across all installation methods (global, local, npx)
  • Standard approach used by TypeScript, ESLint, Prettier, and other major CLIs

Changes

"bundleDependencies": [
  "commander",
  "chalk",
  "@inquirer/prompts"
]

Impact

  • No code changes - TypeScript/JavaScript code remains ESM
  • Works in all environments - Homebrew, nvm, direct Node install
  • Simple and maintainable - Standard npm feature
  • ⚠️ +500KB tarball size - acceptable tradeoff for reliability

Testing

  • Created tarball with npm pack - dependencies bundled correctly
  • Installed globally with npm link
  • Tested CLIs from /tmp (different working directory)
  • Verified module resolution works correctly
  • All unit tests pass

References

Global npm installations (especially with Homebrew) have module resolution
issues with ESM packages. By using bundleDependencies, we ensure that
commander, chalk, and @inquirer/prompts are always available in the
package's node_modules, regardless of installation method.

This is the standard npm solution for CLI packages and is used by
TypeScript, ESLint, and other major CLI tools.

Changes:
- Added bundleDependencies for commander, chalk, @inquirer/prompts
- No code changes required
- Increases tarball size by ~500KB (acceptable tradeoff)
- Works correctly in all installation scenarios

Tested:
- npm pack creates tarball with bundled deps
- CLIs execute correctly from /tmp (different working directory)
- Module resolution works in global installs
Copilot AI review requested due to automatic review settings October 8, 2025 21:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes module resolution issues in global npm installations by bundling CLI dependencies directly in the package tarball. The solution addresses errors where globally installed packages couldn't find their dependencies due to ESM module resolution quirks.

  • Adds bundleDependencies configuration to package.json
  • Bundles CLI-specific dependencies (commander, chalk, @inquirer/prompts) directly in the npm package
  • Uses standard npm approach for CLI packages to ensure reliable dependency resolution

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants