Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/eleven-needles-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/cli": patch
---

fix: resolve bin path from cwd to support monorepo workspace hoisting

`resolveBin()` now walks up from `process.cwd()` to find `node_modules/.bin/<name>`, fixing `MODULE_NOT_FOUND` errors when running `refine dev` in npm/bun/yarn workspaces where packages are hoisted to a parent `node_modules`.
49 changes: 48 additions & 1 deletion packages/cli/src/commands/runner/projectScripts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { vi } from "vitest";
import { vi, afterEach, describe, test, expect } from "vitest";
import path from "path";
import fs from "fs";
import { projectScripts } from "./projectScripts";
import { ProjectTypes } from "@definitions/projectTypes";

Expand Down Expand Up @@ -462,3 +464,48 @@ describe("UNKNOWN project type", () => {
});
});
});

describe("resolveBin (workspace support)", () => {
afterEach(() => {
vi.restoreAllMocks();
});

test("should find binary in cwd node_modules/.bin (standard setup)", () => {
const mockCwd = "/project";
vi.spyOn(process, "cwd").mockReturnValue(mockCwd);
vi.spyOn(fs, "existsSync").mockImplementation((p) => {
return p === path.join(mockCwd, "node_modules", ".bin", "vite");
});

expect(projectScripts[ProjectTypes.VITE].getBin()).toBe(
path.join(mockCwd, "node_modules", ".bin", "vite"),
);
});

test("should find binary in parent node_modules/.bin (monorepo workspace setup)", () => {
const mockCwd = "/project/apps/admin";
const workspaceRoot = "/project";
vi.spyOn(process, "cwd").mockReturnValue(mockCwd);
vi.spyOn(fs, "existsSync").mockImplementation((p) => {
// Binary is NOT in the app's local node_modules, but IS in the workspace root
return p === path.join(workspaceRoot, "node_modules", ".bin", "vite");
});

expect(projectScripts[ProjectTypes.VITE].getBin()).toBe(
path.join(workspaceRoot, "node_modules", ".bin", "vite"),
);
});

test("should find binary for next.js in parent node_modules/.bin (monorepo workspace setup)", () => {
const mockCwd = "/project/apps/admin";
const workspaceRoot = "/project";
vi.spyOn(process, "cwd").mockReturnValue(mockCwd);
vi.spyOn(fs, "existsSync").mockImplementation((p) => {
return p === path.join(workspaceRoot, "node_modules", ".bin", "next");
});

expect(projectScripts[ProjectTypes.NEXTJS].getBin()).toBe(
path.join(workspaceRoot, "node_modules", ".bin", "next"),
);
});
});
22 changes: 22 additions & 0 deletions packages/cli/src/commands/runner/projectScripts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import path from "path";
import fs from "fs";
import { ProjectTypes } from "@definitions/projectTypes";

function resolveBin(name: string) {
// Walk up from the current working directory to find node_modules/.bin/${name}.
// This handles monorepo/workspace setups where packages may be hoisted to a
// parent directory's node_modules.
let dir = process.cwd();
while (true) {
if (process.platform === "win32") {
const exePath = path.join(dir, "node_modules", ".bin", `${name}.exe`);
if (fs.existsSync(exePath)) return exePath;
const cmdPath = path.join(dir, "node_modules", ".bin", `${name}.cmd`);
if (fs.existsSync(cmdPath)) return cmdPath;
} else {
const binPath = path.join(dir, "node_modules", ".bin", name);
if (fs.existsSync(binPath)) return binPath;
}
const parent = path.dirname(dir);
if (parent === dir) break; // reached filesystem root
dir = parent;
}

// Fall back to require.resolve for backward compatibility
if (process.platform === "win32") {
try {
return require.resolve(`.bin/${name}.exe`);
Expand Down
Loading