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.
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.
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.
Update 2023/11/23 :
Next js says this feature is deprecated and you better use environment variables for that usage. But you can still do the same thing with publicRuntimeConfig. For better explanation see this part of their docs.
Here is a "modern" version, which works in next 14, at least:
const { version } = require('./package.json');
module.exports = {
env: {
version
}
};
You can then access version in the app code by process.env.version
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);
import * as pack from '../../package.json'; and then const version = pack.version;javascript)import pack from '../../package.json'; It will show warning with re-casting or importing just the versionpackage.json? Does Next.js extract the version attribute in the build? In this regard, the question isn't a duplicate.