News for hackers. Stuff that matters.
Re:Monorepos in JavaScript and TypeScript by kayson on Thursday June 02, 2022 @01:10PM (#31597659)
I had an extraordinarily hard time getting a monorepo set up for a proof of concept for a pretty basic dashboard app. I was using react for the frontend, node for the backend (Typescript for both), and GraphQL for the API. I tried both npm and yarn for "workspaces" but neither really made things any easier. What I wanted most of all was a repo where both frontend code and backend code were based on the same single-source-of-truth GraphQL schema, so that everything was strongly typed, avoiding any kind of API inconsistencies.

In the end, I never got things working the way I wanted. The hardest thing was getting Typescript (and worse, VScode) to recognize code across modules. The second hardest thing was getting GraphQL schema types into the frontend and backend. There's a huge ecosystem around GraphQL development, especially if you're using JS/TS, but it's still all so clunky. I ended up using a handful of tools to 1) generate the schema from the backend code, 2) serve it via introspection on the backend dev server (thank goodness for hot reloading), and 3) watch said backend schema and generate static type files for the frontend.

Did it work, sure. Is it elegant and straight forward? Definitely not. What a mess!

Re:Monorepos in JavaScript and TypeScript by ThomasRooney on Thursday June 02, 2022 @02:56PM (#31599096)
My experience was very similar: I built an application using a GraphQL schema file that powered AppSync templated VTL/DynamoDB tables, as well as automatically generating GraphQL operations/types. When I cleaned up the application's template for reuse, I erroneously decided to try out Yarn 3/Lerna/PnP, and then lost an embarrassingly long time to make it work.

Each [1] tool [2] seemed [3] to break differently, and needed some form of manual massaging to make it work. That manual massaging meant learning a new configuration file syntax, multiple times.

When it worked, it felt magical. Weaving together an entire web app, powered by a small bit of GraphQL schema [4] means building at a high level of abstraction (hence can be very productive). The only issue is the muddy forest of the NPM ecosystem you're surrounded by: any step towards upgrading your external dependencies seems to cost far more time than promised.

[1] Yarn3/PnP seems to assume all packages define their dependencies correctly. Unfortunately, this isn't true in the real world. I spent hours massaging dependencies in https://github.com/ThomasRooney/reamplify/blob/master/.yarnr...

[2] Getting TypeScript to work cleanly both in an IDE (IntelliJ) and when imported across backend/frontend packages was really cumbersome: I ended up just emitting .gitignored JS files next to their associated TS.

[3] Whispering into the IDE to make it understand GraphQL required learning the .graphqlconfig syntax, and fine-tuning it.

[4] https://github.com/ThomasRooney/reamplify/blob/master/packag...

Re:Monorepos in JavaScript and TypeScript by Alonski on Thursday June 02, 2022 @01:27PM (#31597907)
I recommend taking a look at RedwoodJS
Re:Monorepos in JavaScript and TypeScript by hi on Thursday June 02, 2022 @02:49PM (#31599005)
+1 I've been using RWJS for the past 2 years and would recommend giving it a try:

> yarn create redwood-app my-redwood-project --typescript

Re:Monorepos in JavaScript and TypeScript by kwerk on Thursday June 02, 2022 @08:59PM (#31602847)
+1, I’m early in testing but loving for the reasons you describe.
Re:Monorepos in JavaScript and TypeScript by cersa8 on Friday June 03, 2022 @02:12AM (#31604838)
I had a similar realization that setting up a monorepo with lerna or workspaces was going to be challenging. For a Next.js + custom TypeScript / Node.js backend I decided to just add the backend in its own folder with a child tsconfig.json. This way you can have the backend compile to its own dist folder. No need to fiddle with the complex under the hood bundling that's going on in Next.js. It gives you a single git repo and backend and frontend share the same typings. To make things easier you can setup paths in tsconfig.json and use module-alias in package.json for things like

  import {IUser} from '@shared/interfaces/users/IUser'
Re:Monorepos in JavaScript and TypeScript by taeric on Thursday June 02, 2022 @05:30PM (#31600758)
I'm having PTSD thinking back on doing this with WSDL files back in the day.

Thinking on it, not much different than having a protobuf layer generate data objects across apps. Combined with any client generation logic, we are basically there.

Just, realize that these being separated in repo is also a benefit, since that mirrors how they are separated by deployment.

Re:Monorepos in JavaScript and TypeScript by sbf501 on Thursday June 02, 2022 @04:21PM (#31600037)
Same thing here. I think tooling (packers included) aren't really designed for this, as demonstrated by the hacks you need to apply to aliases and directories. I hope this is fixed over the next few years, because like you, I got it to work but it is fugly.
Re:Monorepos in JavaScript and TypeScript by halfmatthalfcat on Thursday June 02, 2022 @01:45PM (#31598163)
Surprised you haven't heard of or used lerna (with yarn workspaces), which is the defacto monorepo setup in JS world. I use it to maintain a bunch of monorepos and works pretty flawlessly.
Re:Monorepos in JavaScript and TypeScript by dvt on Thursday June 02, 2022 @02:37PM (#31598855)
When people say stuff like this, I genuinely wonder if they actually used lerna. Lerna is more or less a wrapper around yarn/npm, and still 100% totally sucks. Nor does it solve ANY of the problems GP mentions -- which I've also had -- recognizing code from other modules, live-reloading schemas, don't even get me started on jerry-rigging like 8 different webpack plugins because you need sass, but you also need an SVG loader, and also a JSX loader but oops now you're running into some weird conflict between them, etc. etc.

It's honestly embarrassing that modern web development (an incredibly "dumb" network protocol at its core) doesn't have an easier onboarding/development process.

Re:Monorepos in JavaScript and TypeScript by halfmatthalfcat on Thursday June 02, 2022 @08:45PM (#31602664)
Lerna is more akin to loop than it is to a glorified wrapper around yarn/npm, so I question whether it is you who have actually used lerna.

All of the "linking between projects" is exactly what lerna does with bootstrapping/hoisting, so I'm not sure where you're coming from when you say this doesn't address OP's problem.

Re:Monorepos in JavaScript and TypeScript by dvt on Thursday June 02, 2022 @09:46PM (#31603241)
> glorified wrapper around yarn/npm

This is exactly what it is, and it's not even shy about it. It literally calls npm or yarn under the hood (specified with the `npmClient` setting).

> All of the "linking between projects" is exactly what lerna does with bootstrapping/hoisting, so I'm not sure where you're coming from when you say this doesn't address OP's problem.

Yeah, here's a "super simple" lerna example: https://github.com/dan-kez/lerna-webpack-example

It only has about 7 config files before even adding any "real" code. Fun stuff.

Re:Monorepos in JavaScript and TypeScript by halfmatthalfcat on Friday June 03, 2022 @09:39AM (#31608097)
It only relies on npm to do the actual pulling of packages and if you so choose, let yarn do the hoisting (workspaces) but everything else, I fail to see how it's simply a wrapper...

There's a ton of stuff like versioning, linking, running scripts/commands, etc across the entire project that npm and yarn do not inherently support.

I definitely think lerna has warts but it provides a ton of functionality that you'd have to ducktape together on your own, which it looks like a lot of people are trying to do as evident by this entire thread...

Re:Monorepos in JavaScript and TypeScript by andrew_ on Thursday June 02, 2022 @02:11PM (#31598492)
Lerna is one of those "this works great!" or "this is a huge dumpster fire!" solutions. I've seen more broken lerna monorepos than I can count. Recently I've made quite a little side career of fixing lerna monorepos, replacing it wholesale with pnpm's abilities.
Re:Monorepos in JavaScript and TypeScript by douglasisshiny on Thursday June 02, 2022 @02:40PM (#31598893)
I recently discovered pnpm myself and found it to be a significantly better experience. I've been meaning to add something like changesets to it, too.
Re:Monorepos in JavaScript and TypeScript by leetrout on Thursday June 02, 2022 @01:55PM (#31598291)
Was the defacto?

Learna needed a new maintainer and is now moving to Nrwl who develops Nx and seems to be the better choice for future projects?

https://nx.dev/

Re:Monorepos in JavaScript and TypeScript by steve_adams_86 on Thursday June 02, 2022 @02:34PM (#31598823)
They mentioned trying to use Yarn workspaces.

I set up several projects in a monorepo recently using Yarn 3, and while the end result was awesome, the path to success was dark and weedy.

It seemed I was constantly 95% of the way there, but some detail or another wasn't quite right. Yarn has no solution for this, because the problem is the integration of tooling. eslint, typescript, prettier, jest, relay, you name it. They each want to work together and integrate with each other in different ways, using various sources of truth, and so on. Then your IDE needs to adhere to the same protocols, but various extensions have different opinions about default configurations.

Along the way you really need to know your tooling deeply, or you're in for some suffering.

Relatively simple projects with full buy-in of Yarn 3's tooling are probably pretty easy. Once you get relatively complex though, I don't know, I don't think any of this is easy.

It does sound like Nx handles a bunch of this stuff for you, which I'd love to try out. I really like that Yarn isn't particularly opinionated though. Once I had things set up, there was very little to change or that could break. The scaffolding was fairly plain to see, easy to work with, and we owned it. With something like Nx where things reportedly "just work", I worry it would be an unsettling blend of black boxes and black magic making my tooling and code work... Until it didn't, at which point I'd wish I used something less opinionated.

Re:Monorepos in JavaScript and TypeScript by kitten_mittens_ on Thursday June 02, 2022 @06:43PM (#31601476)
You might mean with pnp as the node linker? Migrating to yarn 3 from yarn 1 wasn't an issue at all for me with `nodeLinker: node_modules`.

I just wrapped up switching over to pnp though, and that '95% of the way there' line you mentioned rings a bell for me.

Re:Monorepos in JavaScript and TypeScript by steve_adams_86 on Thursday June 02, 2022 @10:31PM (#31603552)
I did try pnp and in some ways it seemed promising, but it wasn’t the right fit for what we wanted at the time.

You’re right, most of it did work using node_modules as the linker, but I found things like having eslint configure itself from typescript inexplicably blew up and resolving those issues wasn’t trivial.

I mean, it was on and off work for a week or so, migrating a few apps and packages (including a react native app, that was a big part of the motivation and struggle in the migration in the first place), so all in all it was acceptable. And the productivity gain was more than worth it. It just wasn’t as trivial as we expected/hoped.

Re:Monorepos in JavaScript and TypeScript by eezing on Friday June 03, 2022 @10:49AM (#31609059)
True 3 years ago. NPM and Turborepo is all you need today.