【问题标题】:Reactjs process.env.PUBLIC_URL is not workingReactjs process.env.PUBLIC_URL 不工作
【发布时间】:2021-12-27 08:45:47
【问题描述】:

在反应中,我使用下面的代码来加载配置 json,但如果我添加会出错

console.log(process.env.PUBLIC_URL); 

当我删除它时,它可以工作。

错误是

ReferenceError: 未定义进程

const config = {};
export default config;

const SetConfigFile = () => {
    console.log(process.env.PUBLIC_URL);
    switch (process.env.NODE_ENV) {
        case "development":
            return "config.dev.json";
        case "test":
            return "config.test.json";
        case "production":
            switch(process.env.PUBLIC_URL){
                case "https://example.com":
                    return "config.devp.json"
                default:
                    return "config.prod.json";
            }                     
        default:
            return "config.default.json";
    }
}

const Load = () => {
    return fetch(SetConfigFile())
        .then(result => result.json())
        .then((newconfig) => {
            for (let prop in config) {
                delete config[prop]
            }
            for (let prop in newconfig) {
                config[prop] = newconfig[prop];
            }
            return config;
        });
}
export { Load }

为什么我无法在此处访问公共 URL?我错过了什么?

提前致谢

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    我相信您的环境变量的名称必须始终以 REACT_APP 开头

    所以尝试重命名它REACT_APP_PUBLIC_URL

    如果不是这样,这可能与您的 webpack 配置相关联,您可以检查此堆栈溢出答案: https://stackoverflow.com/a/41359607/16956436

    【讨论】:

      猜你喜欢
      • 2016-09-24
      • 2018-10-09
      • 2022-08-07
      • 1970-01-01
      • 2018-10-04
      • 2018-04-23
      • 2017-09-02
      • 2018-05-28
      • 2018-07-21
      相关资源
      最近更新 更多