nuxt.js multiple environment variable configuration

nuxt.js multiple environment variable configuration

1. Introduction

Generally in our project development, there are usually the following three environments

  • The development environment is also called the test environment (test)
  • RC environment is also called pre-release environment ( rc )
  • production environment

2. Scenario

Then there is a situation where we need to distinguish different api interfaces in different environments, for example

  • Test environment (test) api=test.ydhtml.com
  • Pre-release environment ( rc) api=rc.ydhtml.com
  • Online environment (production) api=ydhtml.com

3. Create an environment

Next, we create the env.js file in the root directory of the project with the following content

module.exports = {
    test: {
        MODE: 'test'
    },
    rc: {
        MODE: 'rc',
    },
    prod: {
        MODE: 'prod',
    }
}

After configuring the corresponding environment, we add packaging commands to scripts under package.json .

as follows:

"build:test": "cross-env MODE=test nuxt build",
"build:rc": "cross-env MODE=rc nuxt build",
"build:prod": "cross-env MODE=prod nuxt build",

3.1 Injecting environment variables

Open the nuxt.config.js file and add the following code

const env = require('./env')
module.exports = {
    env: {
        NUXT_ENV: env[process.env.MODE]
    }
}

4. Finally

Finally, we use it in the page. The code is as follows:

const api = {
    prod: 'http://ydhtml.com',
    test: 'http://test-ydhtml.com',
    rc: 'http://rc-ydhtml.com'
}

const baseURL = api[process.env.NUXT_ENV.MODE]

This is the end of this article about nuxt.js multi-environment variable configuration. For more related nuxt.js multi-environment variable configuration content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • New ideas for time formatting in JavaScript toLocaleString()
  • isPrototypeOf Function in JavaScript
  • Detailed explanation of JavaScript prototype chain
  • JavaScript Composition and Inheritance Explained
  • Detailed explanation of js event delegation
  • Differences and usage examples of for, for...in, for...of and forEach in JS
  • Javascript uses the integrity attribute for security verification

<<:  CSS animation property usage and example code (transition/transform/animation)

>>:  Common HTML tag writing errors

Recommend

A practical record of restoring a MySQL Slave library

Description of the situation: Today, I logged int...

The new version of Chrome browser settings allows cross-domain implementation

Preface Currently, the front-end solves cross-dom...

Vue implements the shake function (compatible with ios13.3 and above)

Recently, I made a function similar to shake, usi...

Detailed explanation of the Docker deployment tutorial for Jenkins beginners

This article deploys Jenkins+Maven+SVN+Tomcat thr...

Div exceeds hidden text and hides the CSS code beyond the div part

Before hiding: After hiding: CSS: Copy code The co...

Tips for Mixing OR and AND in SQL Statements

Today, there is such a requirement. If the logged...

Testing of hyperlink opening target

The target attribute of a link determines where th...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

Correct use of Vue function anti-shake and throttling

Preface 1. Debounce: After a high-frequency event...

W3C Tutorial (15): W3C SMIL Activities

SMIL adds support for timing and media synchroniz...

Example of implementing the skeleton screen of WeChat applet

Table of contents What is a skeleton screen How t...

Some things to note about varchar type in Mysql

Storage rules for varchar In versions below 4.0, ...

How to execute Linux shell commands in Docker

To execute a shell command in Docker, you need to...

Nginx learning how to build a file hotlink protection service example

Preface Everyone knows that many sites now charge...