Overview
The @nuxtjs/dotenv module is a Nuxt.js module that allows you to load variables from your .env file directly into your Nuxt.js application context and process.env. It provides an easy way to manage and access environment variables within your Nuxt.js project.
Features
- Loads .env file into context options: The module loads variables from your .env file directly into your Nuxt.js application context and
process.env. - Restrict accessible variables: You can restrict what variables are accessible into the context by passing an
onlyarray with the keys you want to allow. - Customize .env file path: By default, the module loads the .env file from the root of your project, but you can change the path using the
pathoption. - Includes system variables: By default, system variables are ignored, but setting
systemvarsto true allows your system-set variables to work. - Override default .env file name: You can override the default
.envfile name using thefilenameoption.
Installation
- Add
@nuxtjs/dotenvas a dependency to your project:
npm install @nuxtjs/dotenv
- Add
@nuxtjs/dotenvto thebuildModulessection ofnuxt.config.js:
buildModules: [
'@nuxtjs/dotenv',
]
Note: If you are using a Nuxt version previous than v2.9, you have to install the module as a dependency (without --dev or --save-dev flags) and use the modules section in nuxt.config.js instead of buildModules.
Summary
The @nuxtjs/dotenv module provides a convenient way to load variables from your .env file into your Nuxt.js application context and process.env. It allows you to easily manage environment variables and customize their accessibility within your Nuxt.js project. With features like restricting accessible variables, customizing the .env file path, including system variables, and overriding the default .env file name, this module offers flexibility and ease of use for handling environment variables in Nuxt.js.