【问题标题】:turbo/no-undeclared-env-vars not recognizing changesturbo/no-undeclared-env-vars 无法识别更改
【发布时间】:2022-10-23 04:42:14
【问题描述】:

我正在更新我的项目以使用 turborepo,但我遇到了 turbo/no-undeclared-env-vars 的奇怪行为。

在启动项目中,我从环境变量中添加了一个 hello 常量:

export default function Web() {
  const hello = process.env.HELLO;
  return (
    <div>
      <h1>{hello}</h1>
      <Button />
    </div>
  );
}

在运行npm run lint 时,我得到了预期的错误:

web:lint: ./pages/index.tsx
web:lint: 4:17  Error: $HELLO is not listed as a dependency in turbo.json  turbo/no-undeclared-env-vars

但是当我将它添加到 turbo.json 并重新运行 npm run lint 时,它仍然显示错误。

{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build", "$HELLO"],
      "outputs": ["dist/**", ".next/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  }
}

它似乎正在使用缓存,因为如果我从 apps/web/.next/.cache/.eslint 中删除缓存并再次运行它,它就不再显示错误了。

它也以另一种方式工作。 如果我现在从turbo.json 中删除$HELLO 并再次运行npm run lint,它会说没有错误,而应该说它是未列出的。同样在这里,手动删除缓存再次显示它,但在我看来它应该自动检测它,不是吗?

我还尝试更新 turbo.json 在 lint 期间不要使用缓存,但这也无济于事:

{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build", "$HELLO"],
      "outputs": ["dist/**", ".next/**"]
    },
    "lint": {
      "outputs": [],
      "cache": false
    },
    "dev": {
      "cache": false
    }
  }
}

有什么建议么?

【问题讨论】:

    标签: next.js vercel turborepo


    【解决方案1】:

    如果您仍然需要并回答,或者如果有人像我一样并最终在这里搜索问题,解决方案是在构建对象中添加一个名为 env 的新道具,这样您的 turbo.json 将变为

    {
      "$schema": "https://turborepo.org/schema.json",
      "pipeline": {
        "build": {
          "dependsOn": ["^build"],
          "outputs": ["dist/**", ".next/**"],
          "env": [
            "HELLO"
          ]
    
        },
        "lint": {
          "outputs": [],
          "cache": false
        },
        "dev": {
          "cache": false
        }
      }
    }
    

    注意:不需要$ 前缀。

    在 Vercel 上运行 turborepo 时,您还会获得一些额外有用的信息,例如 codemod,通过运行来修复您的 turbo.json 文件

    npx @turbo/codemod migrate-env-var-dependencies
    
    

    在根文件夹中,您将在 json 文件中获得正确的道具。

    最后一件事,如果你使用 next 和 turborepo,你的 ENV 变量,如果像你的例子一样在前端需要它们,应该以 NEXT_PUBLIC_ 为前缀,所以在你的情况下

    const hello = process.env.NEXT_PUBLIC_HELLO;
    

    通过该更改,turborepo 将知道您正在使用该环境变量作为下一个项目的一部分,并且缓存算法将相应地运行

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2018-03-18
      • 2017-07-02
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-17
      相关资源
      最近更新 更多