bun icon indicating copy to clipboard operation
bun copied to clipboard

"bun install" inside the monorepo installs all "node_modules" from "packages" into the root folder

Open Dugnist opened this issue 9 months ago • 9 comments

What version of Bun is running?

1.0.2+37edd5a6e389265738e89265bcbdf2999cb81a49

What platform is your computer?

Linux 6.2.0-32-generic x86_64 x86_64 Ubuntu 22.04.3 LTS

What steps can reproduce the bug?

root package.json:

{
  "name": "monorepo",
  "private": "true",
  "version": "0.0.1",
  "type": "module",
  "workspaces": [
    "packages/*"
  ],
}

I have the packages/one and packages/two structure.

Each package.json look like this:

{
  "name": "one",
  "private": true,
  "version": "0.0.1",
  "module": "index.ts",
  "type": "module",
  "dependencies": {
    "bun-types": "latest",
    "zod": "1"
  }
}

What is the expected behavior?

When I run "bun install" -> I expect to have separated node_modules for each package.

What do you see instead?

When I run "bun install" -> I see shared node_modules for all packages inside a root of a project

Additional information

I write a code to install node_modules from inside each package but it also made a root node_modules folder:

import { promisify } from "node:util";
import { exec } from "node:child_process";

const execAsync = promisify(exec);

// Get list of files inside packages folder
import { getInsideFolderNames } from "../lib/get-project-folder-names/index.ts";

const { listOfPackages, packagesFolderPath } = await getInsideFolderNames(
  "packages"
);

for await (const packageName of listOfPackages) {
  try {
    const packagePath = packagesFolderPath + "/" + packageName;

    const goInsidePackageAndInstallCommand = (
      await execAsync(`cd ${packagePath} && ls -a && bun install`)
    ).stdout;

    console.log(goInsidePackageAndInstallCommand);
  } catch (error: any) {
    console.warn("Ignore: ", error.message);
  }
}

Dugnist avatar Sep 18 '23 18:09 Dugnist

Unless I'm missing something specific about your situation, this is precisely how workspaces are supposed to work. If all of your packages share dependencies, they get hoisted to the root level. Part of the appeal is that you don't need to install shared dependencies into each individual sub-package. https://www.jonathancreamer.com/inside-the-pain-of-monorepos-and-hoisting/

Try this with Yarn or pnpm and you should see the same thing.

colinhacks avatar Sep 20 '23 03:09 colinhacks

@colinhacks in my case, "node_modules" folders are not created inside any package at all. Only the "node_modules" folder at the root of the project. One of the packages uses "fastify", so it definitely has unique dependencies that should not be moved up.

After running pnpm, everything is installed correctly.

btw thanks for the article - it's very useful information)

Dugnist avatar Sep 21 '23 05:09 Dugnist

A bigger problem is that the bun.lockb is messed up. E.g. when installing inside workspace, the root lockfile is overwritten to contain the deps from workspace/package.json, disregarding root package.json entirely.

notramo avatar Sep 29 '23 13:09 notramo

@colinhacks While this is useful for most projects, it is needed to have a way to override this.

For instance, MedusaJS project requires node_modules in its own directory

aldo-roman avatar Oct 14 '23 21:10 aldo-roman

@notramo This happens to me. Please make a separate issue about that. I cannot use bun for production because of that

ryoppippi avatar Oct 18 '23 20:10 ryoppippi

hi, my mono repo has an API with bun and a frontend with next js. The packages were installed on the project root. I got errors when I deployed to vercel because some packages of the API used common JS and next js doesn't support it.

after switching to pnpm everything works fine.

huilensolis avatar Feb 11 '24 18:02 huilensolis

Any news or feedback on this? It's the only thing thats keeps me switching monorepo from pnpm to bun.

mkosir avatar Mar 22 '24 19:03 mkosir

I also would like to have the node_modules in the directory of the app in order to be able to use expo with bun.

janwirth avatar Apr 05 '24 10:04 janwirth

+1 for using Bun with Foundry in a monorepo

Cygnusfear avatar Apr 05 '24 11:04 Cygnusfear