The env option is used to set environment variables for the client side and the server side of your Nuxt app. The default for this option is an empty object, {}. This option is useful when you use axios in your project.
Take the following example:
// nuxt.config.js
export default {
env: {
baseUrl: process.env.BASE_URL || 'http://localhost:3000'
}
}
Then, you can the env property in the axios plugin, as follows:
// plugins/axios.js
import axios from 'axios'
export default axios.create({
baseURL: process.env.baseUrl
})
Now, the baseURL option is set to localhost:3000, or whatever BASE_URL is if it is defined. We can set BASE_URL in package.json, as follows:
// package.json
"scripts": {
"start": "cross-env BASE_URL=https://your-domain-name.com nuxt start"
}
You will need to install cross-env for the preceding example to work on Windows:
$ npm i cross-env --save-dev
We will get to plugins in Chapter 6, Writing...