genversion
So you want yourmodule.version to follow the version in package.json but are tired of updating it manually every time the version changes? Could you just require('./package.json').version or import { version } from './package.json'? That works but for your client side apps, that would bundle the whole package.json and thus expose the versions of your dependencies and possibly other sensitive data too. It is usually a naughty thing to do! How to import only the version? Genversion to the rescue!
YES!!! This is the right answer. Nobody should be shipping their package.json file with their app. — <cite<a href="https://stackoverflow.com/questions/9153571/is-there-a-way-to-get-version-from-package-json-in-nodejs-code/10855054#comment11938061746582508" target="blank"Eric Jorgensen</a</cite
Try it out – Integrate to your build – Command line API – Node API – Contribute – Artwork
Try it out
Usage is simple:
$ cd yourmodule $ npm install genversion $ npx genversion version.js
Voilà! The new version.js:
// Generated by genversion. module.exports = '1.2.3'
Use flags to match your coding style. $ genversion --esm --semi version.js creates:
// Generated by genversion. export const version = '1.2.3';
Need more data? Try $ genversion --property name,version version.js:
// Generated by genversion. exports.name = 'yourmodule'; exports.version = '1.2.3';
Node API is also available:
const gv = require('genversion') gv.generate('lib/version.js', { useSemicolon: true }, (err) = { ... })
See API documentation below for details.
Integrate to your build
The following describes the basic way to use genversion as a part of your workflow. See Advanced integration for alternative ways that might suit you better.