Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process.env.npm_package_version is missing when building for production using vite

Tags:

npm

vite

I'm trying to access process.env.npm_package_version in vite.config.ts. It works in dev, but in production it's says that it's undefined.

export default defineConfig({
    define: {
      __VERSION__: JSON.stringify(process.env.npm_package_version),
    },
});

Having logged process.env, all the NPM env vars are missing in production. Is there any way to access it?

like image 613
Joe Czucha Avatar asked Jan 23 '26 03:01

Joe Czucha


1 Answers

A workaround to get the version would be the following:

import * as fs from 'node:fs'

const packageJson = fs.readFileSync('./package.json', 'utf-8')
const version = JSON.parse(packageJson).version
like image 135
Anton Matiash Avatar answered Jan 25 '26 01:01

Anton Matiash