Skip to content

Proposal: Built-in __VERSION__ Import for Package Version Detection #56828

@aqeelat

Description

@aqeelat

What is the problem this feature will solve?

Many packages need to adapt their behavior based on the installed version of a dependency. Currently, there is no built-in way in Node.js to dynamically determine the version of an imported package at runtime. Developers must resort to workarounds such as reading package.json manually, which is cumbersome and inefficient.

For example, the nest-pino package supports both NestJS 10 and 11. However, NestJS 10 uses Express 4, while NestJS 11 uses Express 5. Because of this, a small change is required:

const DEFAULT_ROUTES = [{ path: '*', method: RequestMethod.ALL }];

Should be:

const DEFAULT_ROUTES = [{ path: '/{*splat}', method: RequestMethod.ALL }];

p.s. I'm not affiliated with that package

What is the feature you are proposing to solve the problem?

A convenient way to read the version from package.json. Something like:

import { __VERSION__ } from 'some-package';
console.log(__VERSION__); // e.g. '1.2.3'

This way, 'nest-pino` can adapt to both version by using:

import { __VERSION__ as nestVersion } from '@nestjs/common';

const DEFAULT_ROUTES = [{ path: nestVersion.startsWith('11') ? '/{*splat}' : '*', method: RequestMethod.ALL }];

The beautiful thing is that this will require zero efforts from package maintainers.
If a package already exports a __VERSION__ variable, then that takes precedence over the one supplied by node.

What alternatives have you considered?

  1. node-pino could release a new version to support NestJS 11. However:
    1. this approach does not scale. Consider a scenario where pino releases a new version that is not backwards compatible. Then pino-js will have to either release two new major versions, or drop support for older versions. This introduces a version matrix that's hard to maintain.
    2. it is a hassle especially when the change is very minimal
  2. using a package like pkginfo, but why add another package for something that node can easily provide?

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.stale

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions