【问题标题】:How to access process.env on Google App Engine with Nodejs如何使用 Nodejs 访问 Google App Engine 上的 process.env
【发布时间】:2019-08-30 14:56:53
【问题描述】:

我在谷歌应用引擎上使用 react。

如文档中所述:https://cloud.google.com/appengine/docs/standard/nodejs/runtime#environment_variables

我可以访问以下环境变量:

Environment variable    Description
GAE_APPLICATION The ID of your App Engine application.
GAE_DEPLOYMENT_ID   The ID of the current deployment.
GAE_ENV The App Engine environment. Set to standard.
GAE_INSTANCE    The ID of the instance on which your service is currently running.
GAE_MEMORY_MB   The amount of memory available to the application process, in MB.
GAE_RUNTIME The runtime specified in your app.yaml file.
GAE_SERVICE The service name specified in your app.yaml file. If no service name is specified, it is set to default.
GAE_VERSION The current version label of your service.
GOOGLE_CLOUD_PROJECT    The GCP project ID associated with your application.
NODE_ENV    Set to production when your service is deployed.
PORT    The port that receives HTTP requests.

当我在谷歌应用引擎上部署我的应用并打印 process.env console.log(process.env) 我只有:

NODE_ENV: "production"
PUBLIC_URL: ""

在文档中明确写了“以下环境变量由运行时设置”,但是没有一个。

怎么了?

【问题讨论】:

  • 我猜你使用的是标准环境,对吧?
  • 不,我使用的是 flex 环境
  • 那么环境变量列表是this one,你引用的是标准环境。不过,您的问题仍然存在。
  • 感谢您为我指出正确的文档,但环境变量仍然是我在问题中报告的两个。

标签: node.js google-app-engine


【解决方案1】:

“以下环境变量由运行时设置”的含义适用于node执行的应用程序。因此,从 express.js 应用程序运行时打印的 process.env 包含这些环境变量,因为在部署 react.js 应用程序时,您将作为 node .bin/wwwnode server.js 执行代码,而不是 react-scripts start。在这种情况下,由 create-react-app 负责设置环境变量。或者,您可以按照create-react-app 规范manually set these environment variables

【讨论】:

  • 它写在我在问题中链接的谷歌文档中。在谷歌应用引擎中,环境在 nodejs 中运行。
  • 是的,实际上可以通过在 App Engine 中部署基本的express 并打印process.env 来观察。所以理论上,由于create-react-app 使用npm,它在技术上仍然使用 nodejs 运行时,因此应该像 expressjs 应用程序一样接收 GAE 设置的环境变量。实际上,事情要复杂得多。 create-react-app 依赖于许多 dependencies,例如 Webpack、Babel 等,并且 env 的配置可能由其中之一决定,优先于 GAE。
  • 所以没什么可做的?
  • 根据我的发现,这根本不可能。 create-react-app 为了简单和实用而创建了大量抽象。例如,浏览器环境中没有环境变量之类的东西。 process.env在浏览器环境中不存在;它是 node.js 特定的。由于它不进行服务器端渲染,因此无法在运行时注入环境变量。相反,Webpack 在构建时将所有出现的process.env 替换为字符串值。本质上,React 中的 env var 与 Node 中的不同,因为它只是一个抽象。
  • 因此,无论是否有意,这更多是库的限制,而不是 App Engine 的错误。随后,任何其他云计算服务提供商(例如 AWS 或 Heroku)都会出现此问题。
猜你喜欢
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多