netlify / ask-netlify

A place to submit questions for Netlify to answer in tutorials, podcasts and blog posts
https://ask.netlify.com
32 stars 8 forks source link

Can I use a yarn monorepo with Netlify? #61

Open nicwaller opened 5 years ago

nicwaller commented 5 years ago

About me

My question in a little more detail

How can Netlify be used with a yarn monorepo where the package being deployed includes other packages in the same repo?

For example, I am developing a React application with a Serverless backend (deployed to AWS Lambda separately), all in TypeScript. They have a bit of shared code which I've put into the shared directory. My file structure looks like this:

packages/
  backend/
  react-app/
    src/
      index.ts
    package.json
  shared/
    index.ts
    package.json

And netlify.toml looks like this:

[build]
  command = "yarn build"
  base    = "packages/react-app"
  publish = "packages/react-app/dist"

When react-app/package.json contains only globally-resolvable modules from npm, everything works fine. But when I add a dependency on the shared module which is only available locally (not published in npm) I get a missing module error.

1:06:54 PM: npm ERR!
1:06:54 PM:  notarget No matching version found for model@1.0.0

Does Netlify support this style of monorepo deployment? If yes, is there a guide somewhere, or plans to develop a guide?

Additional comments for Netlify

I'm a huge fan of Netlify. Recently a coworker asked me to help their teenage child set up a website, and I had them up and running with Netlify in under 15 minutes. Quick and painless!

alexmacavei commented 5 years ago

+1 Yeah I'm interested in something similar 👍

seawatts commented 4 years ago

Any progress here?

rhinodavid commented 4 years ago

Hate to do this but.... +1

ksaturnius commented 4 years ago

For anyone else coming here... You can use Yarn workspaces in a monorepo setup by keeping your base directory set to your project's root directory. This will properly find/link dependencies properly across different package trees as long as your dependencies work in yarn on your local system.

However, this means that if you have multiple sites deploying from the same git repo, you're not able to ignore builds and each site will rebuild even if files in that sub-folder haven't changed. It's not an ideal solution, but it works for now.

[edit] I also discovered a small workaround on another of Netlify's repos that allows you to work more closely to a Yarn/Lerna monorepo setup.

ganonbit commented 4 years ago

same issue still here, pain in the ass to work around.

souljorje commented 4 years ago

Maybe this will help.

  1. Link shared in react-app using yarn link
  2. Leave project root as base
  3. Use yarn workspace command for build:
    [build]
    command = "yarn workspace react-app build"
    publish = "packages/react-app/dist"

More about yarn workspaces