在Shell脚本中为React应用设置环境变量

时间:2019-04-13 14:03:47

标签: reactjs powershell create-react-app yarnpkg

我试图在Powershell(和bash)脚本中设置一些环境变量,并在ReactJS应用程序中读取它们。 Shell脚本很简单:

$env:AUTHDOMAIN="some.domain.com"
$env:AUTHCLIENTID="bunch-o-characters"
$env:AUTHCALLBACK="http://somewhere:3002/callback"
$env:AUTHAUDIENCE="auth-audience"

$env:PORT=3002

# Get-ChildItem Env:

yarn start

yarn正确地拾取了端口,但是React javascript中的PORT都没有可用的变量(包括process.env)。

console.log("domain     : " + process.env.AUTHDOMAIN);
...
domain     : undefined App.js:33

我的package.json文件没有mods:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

我想念什么? Get-ChildItem显示在环境中正确设置了变量。

编辑:我尝试了列出的here解决方案,但该方法无效。它还会阻止终端接收任何输出:

$ ($env:AUTHDOMAIN="some.domain.com") -and (yarn start)

结果相同。

编辑#2:值得注意的是,在linux / bash中,行为完全相同(这是一个很好的石蕊测试方法,因为我已经在bash中进行了大约1000亿次的测试)- yarn正确设置了端口,但没有任何信息使其进入react应用程序:

#!/bin/sh

export AUTHDOMAIN="some.domain.com"
export AUTHCLIENTID="bunch-o-text"
export AUTHCALLBACK="http://somewhere:3001/callback"
export AUTHAUDIENCE="auth-audience"

export PORT=3002

yarn start

因此,这可能与powershell无关。

1 个答案:

答案 0 :(得分:1)

您要使用的每个环境变量都必须以$('.ace_text-input').keyup(function(e) { delay(function(){ var html1 = buildSource(html, js, css); var iframe = document.createElement('iframe'); iframe.src = 'about:blank'; iframe.frameBorder="0"; iframe.height = 496; iframe.css='width:100%;'; iframe.className = 'preview-iframe'; $('.preview-iframe').remove(); $('div#preview-container').append(iframe); iframe.contentWindow.document.open('text/html', 'replace'); iframe.contentWindow.document.write(html1); iframe.contentWindow.document.close(); },1000); }); 为前缀 按照{{3}}:

  

注意:您必须创建以REACT_APP_开头的自定义环境变量。除NODE_ENV以外的任何其他变量都将被忽略,以避免意外地在机器上公开可能具有相同名称的私钥。更改任何环境变量都将要求您重新启动开发服务器(如果正在运行)。

相关问题