Using serverless with yarn workspaces

I am trying to setup a monorepo for a large serverless project. When using yarn workspaces a node_modules folder is created in the root of the repo. All of the shared modules are hoisted into this node_modules and the node_modules folder in the individual serverless applications only contain the unique versions of specific modules. When I deploy or package my application all of the modules that are in the root node_modules folder are missing.

Is there a way to tell serverless to look in each node_modules directory until it hits the repository root?

thanks,

Luke

I ran into this as well. I don’t think Serverless has a way to handle this itself, so I had to disable the hoisting feature of Yarn Workspaces:

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

See here for more info on nohoist.

This is very suboptimal but I had to go with Ryan’s advice too.

I made it a little more specific:

"nohoist": [
      "**/serverless-*/**"
]

But there are significant issues, most notably that dev dependencies are bundled with your app: https://github.com/serverless/serverless/pull/3889#issuecomment-414547166

We use Yarn Workspaces at our company (butterwire) as all our JS code is in a mono repo.

We created a small plugin to help use serverless without requiring to use nohoist, for the reasons mentioned above. It might have some rough edges but hopefully it’s useful to some.

2 Likes

Thank you!!! This is what I’ve been looking for!

Does anyone have an example of how to create a project with yarn workspaces and serverless-plugin-monorepo?

Although I’ve had success using serverless-plugin-monorepo packaging up the hoisted dependencies, I’m still running into issues packaging up local packages not published to an external repo.

Local builds work fine, but serverless package errors with:

Error --------------------------------------------------

  yarn install --frozen-lockfile --non-interactive failed with code 1
  error An unexpected error occurred: "https://registry.yarnpkg.com/@private-package/foo: Not found".

I’m using serverless-webpack and it seems the issue lies with that plugin? I switched to serverless-jetpack and it packaged and deployed correctly, but I’d prefer to manage the packaging of the lambda with webpack for minification etc if possible.

Hi DanRivett, I’m also facing the same issue. After switching to serverless-jetpack, it seems bundling is working however, I’m not able to apply babel to convert the ES6 code to commonjs and so it fails during runtime. Any thoughts on how to do that? With webpack such things were explicit configurations.