【问题标题】:How to specify an URL in .env file of Gatsby app?如何在 Gatsby 应用程序的 .env 文件中指定 URL?
【发布时间】:2021-02-27 17:29:15
【问题描述】:

以防万一,我想知道 Gatsby 应用程序的设置是否与其他基于节点的应用程序不同。

我想通过将http://localhost:1337 移动到.env 来重构gatsby-config.js

来自

plugins: [
  {
    resolve: `gatsby-source-strapi`,
    options: {
      apiURL: `http://localhost:1337`,
    },
  },
]

plugins: [
  {
    resolve: `gatsby-source-strapi`,
    options: {
      apiURL: process.env.STRAPI,
    },
  },
]

.env 对我不起作用:

STRAPI=$(http://localhost:1337)

【问题讨论】:

    标签: environment-variables gatsby dotenv


    【解决方案1】:

    在项目的根目录中创建一个名为 .env.development.env.production 的文件。在那里,创建你的变量,只需:

    STRAPI= "http://localhost:1337"
    

    在您的gatsby-config.js 中添加以下 sn-p(在模块导出上方):

    require("dotenv").config({
      path: `.env.${process.env.NODE_ENV}`,
    })
    

    最后,留下配置:

    plugins: [
      {
        resolve: `gatsby-source-strapi`,
        options: {
          apiURL: process.env.STRAPI,
        },
      },
    ]
    

    默认情况下,Gatsby 在分别运行gatsby developgatsby build 时将采用.env.development.env.production,允许您将环境变量传递给服务器文件(gatsby-config.js 等)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2020-09-25
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2021-10-25
      • 2014-11-02
      相关资源
      最近更新 更多