Tho*_*mas 4 next.js vercel turborepo
我正在更新我的项目以使用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并再次运行它,它就不再显示错误了。
反之亦然。如果我现在删除$HELLO并再次turbo.json运行,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
    }
  }
}
有什么建议么?
如果您仍然需要并回答,或者如果有人像我一样最终在这里谷歌搜索问题,解决方案是在名为的构建对象中添加一个新的道具,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
    }
  }
}
注意:$不需要前缀。
turbo.json在 Vercel 上运行 Turborepo 时,您还可以获得一些额外有用的信息,例如通过运行来修复您的文件的codemod
npx @turbo/codemod migrate-env-var-dependencies
在根文件夹中,您将在 json 文件中获得正确的 props。
最后一件事,如果您使用 next 和turborepo,您的 ENV 变量(如果像您的示例一样在前端需要它们)应该NEXT_PUBLIC_在您的情况下以 so为前缀
const hello = process.env.NEXT_PUBLIC_HELLO;
通过这一更改,turborepo 将知道您正在使用该环境变量作为下一个项目的一部分,并且缓存算法将相应地运行
| 归档时间: | 
 | 
| 查看次数: | 4843 次 | 
| 最近记录: |