Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]无法配置自定义的环境变量 #7799

Closed
Wangggym opened this issue Jan 18, 2022 · 9 comments
Closed

[Bug]无法配置自定义的环境变量 #7799

Wangggym opened this issue Jan 18, 2022 · 9 comments

Comments

@Wangggym
Copy link

What happens?

配置的环境变量没有生效
image
image
控制台中无法打印出 自定义的环境变量
image

Mini Showcase Repository(REQUIRED)

Please provide a repository using yarn create @umijs/umi-app then upload to your GitHub 请提供一个可复现的仓库,使用 yarn create @umijs/umi-app 创建,并上传到你的 GitHub 仓库

How To Reproduce

Steps to reproduce the behavior: 1. 2.

Expected behavior 1. 2.

Context

  • Umi Version:
  • Node Version:
  • Platform:
@Wangggym Wangggym changed the title [Bug] say something [Bug]无法配置自定义的环境变量 Jan 18, 2022
@jk4235
Copy link

jk4235 commented Mar 10, 2022

你可以创建两个和.umirc.ts平级的文件, .umirc.dev.ts.umirc.prod.ts

文件内定义你不同环境下的process.env

example:

import { defineConfig } from "umi";

export default defineConfig({
  define: {
    "process.env": {
      SOME_KEY: "value",
      APP_COOKIE_TOKEN: 123456,
    },
  },
});

之后修改你的package.json命令

build: UMI_ENV=prod umi build
start: UMI_ENV=dev umi build

当然, dev, prod名称是可以替换的

@yzwxk
Copy link

yzwxk commented Mar 23, 2022

@jk4235 为什么不能直接使用process.env 需要在define里设置呢

@jk4235
Copy link

jk4235 commented Mar 25, 2022

umi中的环境变量列表只有这些

想要自定义的环境变量, 这是我找到的一个workaround方法

而且可以保持类型,而不是都是字符串

@ldwonday
Copy link

设置一个环境变量真是麻烦,绕来绕去的,搞的我花了一个多小时才搞明白,真是烦躁,直接通过coss-env设置自己想要的环境变量和值有那么难吗,谁搞的现在的这种设计,拉出来打一顿算了。

@peter064226
Copy link

peter064226 commented Jun 24, 2022

设置一个环境变量真是麻烦,绕来绕去的,搞的我花了一个多小时才搞明白,真是烦躁,直接通过coss-env设置自己想要的环境变量和值有那么难吗,谁搞的现在的这种设计,拉出来打一顿算了。

深有体会,这个玩意真是好麻烦啊,环境变量本身很简单的东西弄的这么麻烦,弄了好久也不生效,可以组团打他一顿。

@sorrycc sorrycc closed this as completed Jul 4, 2022
@tinyzh
Copy link

tinyzh commented Sep 1, 2022

找了快一天,发现UMI_ENV不能定义为dev,prod,test,得自定义自己的环境变量
image
image
image
然后项目里面直接用environment就可以了

@luoyelusheng
Copy link

最新简洁用法,直接在.umirc中加上这一行,直接可以使用cross-env环境变量,无需定义多个配置文件
define: { "process.env": { ...process.env, }, },

@littcc
Copy link

littcc commented Feb 22, 2023

最新简洁用法,直接在.umirc中加上这一行,直接可以使用cross-env环境变量,无需定义多个配置文件 define: { "process.env": { ...process.env, }, },

有效,这个设计相当奇怪。。。

@mdddj
Copy link

mdddj commented Jul 19, 2024

umi4 怎么设置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants