Skip to content

Commit bc2fb63

Browse files
committed
manually add the .pnpm/node_modules to NODE_PATH if in a pnpm repo
1 parent 4ba943b commit bc2fb63

File tree

1 file changed

+37
-3
lines changed
  • packages/cli-v3/src/commands

1 file changed

+37
-3
lines changed

packages/cli-v3/src/commands/dev.tsx

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
parseBuildErrorStack,
5252
parseNpmInstallError,
5353
} from "../utilities/deployErrors";
54+
import { findUp, pathExists } from "find-up";
5455

5556
let apiClient: CliApiClient | undefined;
5657

@@ -465,7 +466,7 @@ function useDev({
465466
const environmentVariablesResponse =
466467
await environmentClient.getEnvironmentVariables(config.project);
467468

468-
const processEnv = gatherProcessEnv();
469+
const processEnv = await gatherProcessEnv();
469470

470471
const backgroundWorker = new BackgroundWorker(fullPath, {
471472
projectConfig: config,
@@ -775,7 +776,7 @@ function createDuplicateTaskIdOutputErrorMessage(
775776
return `Duplicate ${chalkTask("task id")} detected:${duplicateTable}`;
776777
}
777778

778-
function gatherProcessEnv() {
779+
async function gatherProcessEnv() {
779780
const env = {
780781
NODE_ENV: process.env.NODE_ENV ?? "development",
781782
PATH: process.env.PATH,
@@ -786,11 +787,44 @@ function gatherProcessEnv() {
786787
NVM_BIN: process.env.NVM_BIN,
787788
LANG: process.env.LANG,
788789
TERM: process.env.TERM,
789-
NODE_PATH: process.env.NODE_PATH,
790+
NODE_PATH: await amendNodePathWithPnpmNodeModules(process.env.NODE_PATH),
790791
HOME: process.env.HOME,
791792
BUN_INSTALL: process.env.BUN_INSTALL,
792793
};
793794

794795
// Filter out undefined values
795796
return Object.fromEntries(Object.entries(env).filter(([key, value]) => value !== undefined));
796797
}
798+
799+
async function amendNodePathWithPnpmNodeModules(nodePath?: string): Promise<string | undefined> {
800+
const pnpmModulesPath = await findPnpmNodeModulesPath();
801+
802+
if (!pnpmModulesPath) {
803+
return nodePath;
804+
}
805+
806+
if (nodePath) {
807+
if (nodePath.includes(pnpmModulesPath)) {
808+
return nodePath;
809+
}
810+
811+
return `${nodePath}:${pnpmModulesPath}`;
812+
}
813+
814+
return pnpmModulesPath;
815+
}
816+
817+
async function findPnpmNodeModulesPath(): Promise<string | undefined> {
818+
return await findUp(
819+
async (directory) => {
820+
const pnpmModules = join(directory, "node_modules", ".pnpm", "node_modules");
821+
822+
const hasPnpmNodeModules = await pathExists(pnpmModules);
823+
824+
if (hasPnpmNodeModules) {
825+
return pnpmModules;
826+
}
827+
},
828+
{ type: "directory" }
829+
);
830+
}

0 commit comments

Comments
 (0)