Using serverless with yarn workspaces


#1

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


#2

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.


#3

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


#4

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.