vue.js Nuxt 3开发/生产环境

kjthegm6  于 6个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(101)

我正在尝试在Nuxt 3中设置开发/生产环境。原因是一些测试。如果应用程序URL以develop-,staging-开头,或测试─我想使用一个测试端点,如https://develop-www.app.com/。我有我的nuxt配置设置,它的工作时,我在本地运行,并在prod通过创建变量关闭NODE环境。然而,当在任何测试环境中前面提到的应用程序正在使用PROD端点,因为它不知道它是一个开发环境。有没有一种方法可以基于NODE env设置它?

nuxt config

const isProdEnv = process.env.NODE_ENV === 'production';
const testServicesEndpoint =
  'https://test-services.location';
const prodServicesEndpoint =
  'https://services.prodlocation';
const servicesEndpoint = isProdEnv
  ? prodServicesEndpoint
  : testServicesEndpoint;

export default defineNuxtConfig({
     runtimeConfig: {
        public: {
          apiBase: servicesEndpoint,
        },
      },
})

字符串
我创建了一个generateTest动作,如果分支不等于main,它将在github动作中运行,我认为这是可行的,但构建显示了这个错误:
[警告]将NODE_ENVdevelopment更改为production,以避免意外行为。

"scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "generateTest": "NODE_ENV=development nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },

toe95027

toe950271#

当Nuxt构建dist时,它不会知道当前的域,因为没有请求发送到Nuxt。
多个.env文件可以满足您的需求。
例如,在Nuxt项目根目录中,

.env.test
.env.stage
.env.prod
.env

字符串
特别是package.json中的ENV
.env的自定义文件

"scripts": {
        "build": "nuxt build",
        "dev": "nuxt dev",
        
        // for different env variables
        "dev:test": "nuxt dev --dotenv .env.test", // run with .env.test
        "dev:stage": "nuxt dev --dotenv .env.stage", // run with .env.stage
        
        "generate": "nuxt generate",
        "preview": "nuxt preview",
        "postinstall": "nuxt prepare",
        "lint": "eslint .",
        "lint:fix": "eslint . --fix",
        "prod": "node .output/server/index.mjs"
    },

相关问题