3

试图让 Heroku 使用工作区为 Yarn v3 使用缓存模块。我有以下内容:

package.json

"engines": {
    "node": "16.x",
    "yarn": "3.x"
  },
  "cacheDirectories": [
    "node_modules",
    "packages/components/node_modules",
    "packages/lib/node_modules",
    "packages/schema/node_modules",
    "packages/web/node_modules",
    "packages/web/.next/cache"
  ]

heroku-buildpack-features

cache-native-yarn-cache=true

Heroku 输出:

-----> Creating runtime environment
       
       NPM_CONFIG_LOGLEVEL=error
       USE_YARN_CACHE=true
       NODE_VERBOSE=false
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       
-----> Installing binaries
        !     You don't need to specify Yarn engine. Heroku will install the latest Yarn 1.x, so that per project version can be used. More information here: https://yarnpkg.com/getting-started/install#global-install
              https://devcenter.heroku.com/articles/nodejs-support
       
       engines.node (package.json):  16.x
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.7.0...
       Using default npm version: 7.20.3
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.11)
       Using yarn 3.0.0

-----> Installing binaries
        !     You don't need to specify Yarn engine. Heroku will install the latest Yarn 1.x, so that per project version can be used. More information here: https://yarnpkg.com/getting-started/install#global-install
              https://devcenter.heroku.com/articles/nodejs-support
       
       engines.node (package.json):  16.x
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)
       
       Resolving node version 16.x...
       Downloading and installing node 16.7.0...
       Using default npm version: 7.20.3
       Resolving yarn version 1.22.x...
       Downloading and installing yarn (1.22.11)
       Using yarn 3.0.0
       
-----> Restoring cache
       Loading 6 from cacheDirectories (package.json):
       - node_modules
       - packages/components/node_modules (not cached - skipping)
       - packages/lib/node_modules
       - packages/schema/node_modules
       - packages/web/node_modules
       - packages/web/.next/cache

<a bunch of resolve stuff>

       ➤ YN0000: ┌ Fetch step
       ➤ YN0013: │ @apollo/client@npm:3.4.7 can't be found in the cache and will be fetched from the remote registry
       ➤ YN0013: │ @apollo/protobufjs@npm:1.2.2 can't be found in the cache and will be fetched from the remote registry

<and so on>

我试过添加".yarn/cache"cacheDirectoriesin package.json,但它总是说它是空的。

不太确定从这里去哪里。

4

1 回答 1

4

截至 2021 年 9 月 2 日,Heroku 的 nodeJS buildback 不完全支持 yarn 2。他们开始在v173中添加支持,但尚未完全解决缓存问题。

根本问题是buildpack没有正确检测到它的$BUILD_DIR/.yarn/cache存在并将环境变量设置YARN_CACHE_FOLDER.yarn/cache. L140 )

当 buildpack 到达缓存步骤时,该.yarn/cache目录实际上并不存在。

解决方案:

我可以通过在config varsYARN_CACHE_FOLDER中手动设置来解决这个问题。您可以通过 CLI 运行..yarn/cacheheroku config:set YARN_CACHE_FOLDER=.yarn/cache

确保您也已.yarn/cachecacheDirectories.

当构建日志显示时,您将知道它有效:

-----> Caching build
       Saving 1 cacheDirectories (package.json):
       - .yarn/cache

(nothing to cache)如果它不起作用,旁边会有一个注释.yarn/cache

于 2021-09-03T00:17:36.253 回答