Skip to content

Read the --version string from package.json - #19

Merged
kev1n merged 1 commit into
mainfrom
version-from-package-json
Sep 27, 2026
Merged

kev1n merged 1 commit into
mainfrom
version-from-package-json

Conversation

@kev1n

@kev1n kev1n commented Sep 27, 2026

Copy link
Copy Markdown
Member

What changes

  • src/version.ts (new) exports VERSION, read at runtime from the package's own package.json with createRequire(import.meta.url)('../package.json'). This is the same pattern getanyapi-com/mcp uses in its src/version.ts.
  • src/index.ts passes VERSION to commander's .version() instead of the hard-coded "0.8.0". No hand-kept version string remains in src/.
  • __tests__/version.test.ts (new) runs the real entrypoint with --version, captures what it prints, and fails if that differs from package.json.

No version bump, no publish, no changelog entry.

Why

src/index.ts:34 hard-coded .version("0.8.0"), so the published 0.9.1 prints 0.8.0. Measured across releases, the printed version was wrong in 7 of 15. Every release needed a second manual edit, and it was missed about half the time.

../package.json resolves from both src/ (tests) and dist/ (the published build), because both sit one level below the package root. npm always includes package.json in the tarball. The lookup goes through the module's own URL, not the working directory, so the installed CLI reports its own release wherever it is run from.

Proof

The test fails on the old code (new test only, src/index.ts unchanged):

AssertionError: expected '0.8.0\n' to be '0.9.1\n' // Object.is equality
 Test Files  1 failed (1)

With the fix:

 Test Files  1 passed (1)
      Tests  1 passed (1)

Repo gate (npm test && npm run build, from AGENTS.md):

 Test Files  12 passed (12)
      Tests  102 passed (102)
> anyapi-cli@0.9.1 build
> tsc -p tsconfig.json
build_exit=0

Packed artifact. I ran npm pack, installed the tarball into an empty temp project created by npm init -y (whose own package.json says 1.0.0), then ran the installed binary:

$ npm pack --pack-destination /tmp/wf-cli-version-pack
npm notice 489B dist/version.js
npm notice 889B package.json
npm notice filename: anyapi-cli-0.9.1.tgz
$ cd /tmp/wf-cli-version-pack/install && npm install ../anyapi-cli-0.9.1.tgz
added 3 packages in 353ms
$ ./node_modules/.bin/anyapi --version
0.9.1
$ node -p "require('./package.json').version"   # the temp project's own manifest
1.0.0

The installed CLI prints its own 0.9.1, not the old 0.8.0 and not the surrounding project's 1.0.0.

🤖 Generated with Claude Code

src/index.ts hard-coded .version("0.8.0"), so the published 0.9.1 printed
0.8.0. src/version.ts now reads the version from the package's own
package.json (the same createRequire pattern getanyapi-com/mcp uses), which
npm always packs, so the installed CLI reports the installed release.

The new test runs the real entrypoint with --version and fails if what it
prints differs from package.json; it failed on the old code (0.8.0 vs 0.9.1).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@kev1n
kev1n merged commit fb283ae into main Sep 27, 2026
2 checks passed
@kev1n
kev1n deleted the version-from-package-json branch September 27, 2026 01:56
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.

1 participant