Home > database >  How to get version from package.json in next js?
How to get version from package.json in next js?

Time:11-10

I am trying to get the "version" from the package.json is nextjs application.

I have tried process.env.npm_package_version like in node application but it's returning undefined.

CodePudding user response:

You can use process.env.npm_package_version only if you installed the package package_vars before (see: https://docs.npmjs.com/cli/v6/using-npm/scripts#packagejson-vars)

But the simplest way in your case is to import your package.json file (which is a mere .json file) like this:

const { version } = require('./package.json');
console.log(version);

CodePudding user response:

in NextJs you can create a next.config.js file and below lines.

const { version } = require('./package.json');

module.exports = {
  publicRuntimeConfig: {
    version,
  },
};

then when you need the version anywhere in app

import getConfig from 'next/config';

const { publicRuntimeConfig } = getConfig();
const version = publicRuntimeConfig?.version

for more info read this article.

  • Related