ps-aux
ps-aux

Reputation: 12146

How to retrieve version of my npm application without importing other package.json info?

I have an SPA application (in React) built with Webpack (as part of create-react-app) where I display a version number retrieved from the package.json likes this:

import npmInfo from 'package.json'
...
<div className="version">{npmInfo.version}</div>

However this results in also other package.json being part of built javascript file and exposed to the public.

Is there an approach to retrieve just the version from package.json other than reading it in parent the process and setting it to environment variable (and reading it via process.env.VERSION) ?

Upvotes: 1

Views: 625

Answers (1)

macbem
macbem

Reputation: 1230

I'm not sure if this will solve your problem, as you are a bit against using (or rather setting?) environment variables, but if your application is ran or compiled by a npm script, you could use process.env.npm_package_version.

Note: this will only be available if you use npm scripts.

Upvotes: 1

Related Questions