Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow runtime detection of the got version #2385

Closed
osher opened this issue Oct 20, 2024 · 4 comments
Closed

Allow runtime detection of the got version #2385

osher opened this issue Oct 20, 2024 · 4 comments

Comments

@osher
Copy link

osher commented Oct 20, 2024

What problem are you trying to solve?

  • I'm modernizing some codebase that used got@10, migrating to got@14.
  • That codebase is composing user-agent header strings that included the version of got among other things like the version of the service and the runtime version of node.
  • we do not use package-lock.json, and we want to reflect the version that landed in the distribution.
  • I used to do that by require('got/package').version, however, since the introduction of the package.json:exports section - I cannot do that anymore 😞

Describe the feature

Allow runtime detection of the got version.

Two ways to do that:

  • got should export a version field, that should reflect the version field in the package.json

e.g.:

import { pkg: { name, version } } from '../config.js'  //<-- reads from local package.json
import got from `got`

const { version: gotVersion } = got

const userAgentHeader = `${name}/${version}; nodejs/${process.version}; got/${gotVersion}`

or, alternatively

  • allow to import the package.json
import { pkg: { name, version } } from '../config.js'  //<-- reads from local package.json
import gotPackage from `got/pacakge.json`
const { version: gotVersion } = gotPackage

const userAgentHeader = `${name}/${version}; nodejs/${process.version}; got/${gotVersion}`

Checklist

  • [v] I have read the documentation and made sure this feature doesn't already exist.
@osher
Copy link
Author

osher commented Oct 20, 2024

If you would like to accept this feature, and tell me which way you prefer - I'll be happy to submit a PR.

However - I might need help in onboarding: I cloned the repo, ran npm install && npm run test and the test suite does not exit, and I see failures.

node v20.17.0, npm 10.8.2, Fedora 39 in WSL.

reproduced on the Windows host as well with node v20.12.2 and npm 10.5.0.

💡
Or I can skip running tests locally, and work until I get the build to pass 🤷 😅

@sindresorhus
Copy link
Owner

I'm not interested in adding any kind of version export. This should be solved by your bundler instead. It's not a problem specific to this package. It applies to every package with an exports field. I also believe Node.js is working on a method to get the package.json of a specific package.

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Oct 20, 2024
@osher
Copy link
Author

osher commented Oct 20, 2024

I see. Thanks for the response.

Well. I'm writing server code in plain JS, I do not have any bundler.

So the solution would be to locate the node_modules, find got/package.json up the import path, and read it manually.... 😝

@osher
Copy link
Author

osher commented Oct 20, 2024

mm!

sorry for the hassle! Looks like the solution is much simpler than I thought.

Sharing:

import { readFileSync } from 'node:fs'
const gotPath = import.meta.resolve("got").slice(6) // remove 'file:/' prefix
const pkgPath = gotPath.replace(/\/got\/.*/, '/got/package.json')
const { version } = JSON.parse( readFileSync( pkgPath ) )

Add some error-handling, and there you go 🙂

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

No branches or pull requests

2 participants