Firebase Functions with Yarn workspaces

后端 未结 2 1855
耶瑟儿~
耶瑟儿~ 2021-02-05 14:33

We\'re starting to adopt a monorepo setup using yarn workspaces and we\'d like to have our firebase functions inside it. The repo structure is something like:

re         


        
相关标签:
2条回答
  • 2021-02-05 15:00

    The solution I found for this is Yarn's nohoist option in your root package.json file.

    By default Yarn hoists dependencies to the root directory so they can be shared between your packages. Unfortunately this will not work with Firebase. This means you need to tell Yarn not to hoist the dependencies used by your Firebase functions.

    The documentation for nohoist is less than ideal, but here is an official blog post about it here: https://yarnpkg.com/blog/2018/02/15/nohoist/

    You probably want something like this:

    {
      "workspaces": {
        "packages": [
          "packages/*"
        ],
        "nohoist": [
          "functions/core",
          "functions/common",
          "functions/**"
        ]
      }
    }
    

    Keep in mind that this uses the name field used in the package.json files of each workspace package. So in this example, it is assume that the functions directory has a package.json with "functions" as it's name.

    functions/** tells yarn not to hoist any of the dependencies specified in packages/functions/package.json. This doesn't work for your shared yarn packages though, so functions/core and functions/common need to be specified separately.

    You also need to include your workspaces as dependencies in your functions project, so add them to your package.json:

    {
      "name": "functions",
      "dependencies": {
        "core": "*",
        "common": "*",
      }
    }
    

    Once you have added all that, you should delete your packages/functions/node_modules directory and run yarn install. After doing this, you should see all your dependencies included in packages/functions/node_modules (not symlinks).

    0 讨论(0)
  • 2021-02-05 15:01

    I am not sure I understand the question exactly, but I could give you my two cents on yarn workspaces based on whatever I understood from your question and from my experience using it.

    Yarn workspaces consolidate all your dependencies into the node_modules present in project root as well as in a single package-lock.json to reduce conflicts and enables yarn to optimize the installation process giving you a faster yarn install. And also another advantage of it is, with a single pass yarn install can install dependencies of all packages under the workspace.

    Edit: I think for some reason yarn link is not being called and instead only yarn install is being run, which will search the npm registries and throws the error mentioned in comment since it can't find the mentioned package on npm registry. So for a solution try creating an entry in the firebase's package.json like

    "dependencies": {
      "a": "file:../dependency-package-name/",
    }
    
    0 讨论(0)
提交回复
热议问题