Eric Dela Cruz
Eric Dela Cruz

Reputation: 1761

How to set an env variable during build that is coming from package.json

What do I need to add to nuxt.conf.js to get the version from package.json and store the value in an environment variable?

I want to set the version of package.json in a vuex store like:

state: {
  version: process.env.version
}

I tried putting this in nuxt.conf.js but that didn't work.

 env: {
   version: JSON.stringify(require('./package.json').version)
 }

I know how to do this with Quasar as in quasar.conf.js

build: {
  env: {VERSION: JSON.stringify(require('./package.json').version)}
}

So how do achieve the same effect with nuxt?

Upvotes: 1

Views: 1056

Answers (1)

Eric Dela Cruz
Eric Dela Cruz

Reputation: 1761

env: {
 version: JSON.stringify(require('./package.json').version)
}

This actually works but only when you restart a build. I was expecting it to work dynamically but it didn't. It's not really a problem, it's only needed when you do a production build.

In my package.json I have a build script like this:

"build": "nmp version patch && nuxt build"

This will increase the version number that I display in my nuxt app. I always forgot to do this manually so I automated this.

Upvotes: 2

Related Questions