dotenv configuration multi-environment support vue, nuxtjs and webpack project packaging closure guide

The file name defaults to .env
when we can name it
.env.test
.env.dev
.env.prod
Note that there should be no quotation marks in the file
DB_HOST=localhost
DB_USER=root

ps1: So how do we make our variables support server and client in nuxtjs?
You can define the env variable again in nuxt.config.js and give the value to the past
env: { COOKIE_DOMAIN: process.env.COOKIE_DOMAIN, } ps2: module trial method in nuxtjs // ['@nuxtjs/dotenv', { filename: }] , here can be path or file or filename ps3: If you need to use the .env variable require('dotenv').config in the configuration file, the official also has instructions. But pay attention to closing the pit. The path and debug parameters here can be entered by yourself. The path needs to be the full path, otherwise the variables of the .env file will be obtained by default, and the content cannot be overwritten.



.env.${process.env.NODE_ENV}



[dotenv][DEBUG] “COOKIE_DOMAIN” is already defined in process.env and
will not be overwritten [dotenv][DEBUG] “LOGINMODE” is already defined
in process.env and will not be overwritten

So the solution:
import path from 'path'; // This step must be done, otherwise an error will be reported
// There are many ways to obtain the real directory address of the system
const rootPath = path.resolve(process.cwd(), .env.${process.env.NODE_ENV});
then Switch different configurations according to our current environment to get
require('dotenv').config({ path: rootPath })

Plug-in address: https://www.npmjs.com/package/dotenv
Project source code address: https://gitee.com/shuogesha/boot-mongo-admin

Guess you like

Origin blog.csdn.net/zhaohaiyuan123/article/details/120562040