I am trying to access from browser the package.json version
In Nx, it is possible to hardcode env vars in a .env file and have them passed to process.env.{varname}
if the variables are prefixed vith NX_
.
I can't hardcode the variable though, I need to read it from package.json.
Anyone knows how to do that?
In a Nx workspace, the custom webpack configuration file can be named according to your preference, as you will reference it explicitly in the
angular.json
orworkspace.json
file. A common convention for naming this file iswebpack.config.js
orcustom-webpack.config.js
.See "Configure webpack on your Nx workspace"
In your webpack configuration file, you can import the
package.json
:Use the
DefinePlugin
in your webpack configuration to define the variable based onversion
.See "Using environment variables in Angular applications".
In your custom webpack configuration, it might look like this:
Update your application's build options to use the custom webpack configuration. For example, if you are using Angular, you can update the
angular.json
file:You should now be able to access the version in your code like this: