Skip to content

Commit 5e28dea

Browse files
fix(cli): fix ENOWORKSPACES when running any command on latest npm (#2457)
* fix(cli): fix `ENOWORKSPACES` when running any command on latest npm * fix `npm config` for monorepos Co-authored-by: Szymon Rybczak <[email protected]> * chore: run linter and apply auto-fixes --------- Co-authored-by: Szymon Rybczak <[email protected]>
1 parent d6982fc commit 5e28dea

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

packages/cli/src/tools/npm.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ export function isProjectUsingNpm(cwd: string) {
2929
return findUp.sync('package-lock.json', {cwd});
3030
}
3131

32-
const registry = getNpmRegistryUrl();
32+
export const getNpmRegistryUrl = (() => {
33+
// Lazily resolve npm registry url since it is only needed when initializing a
34+
// new project.
35+
let registryUrl = '';
36+
return () => {
37+
if (!registryUrl) {
38+
try {
39+
registryUrl = execSync(
40+
'npm config get registry --workspaces=false --include-workspace-root',
41+
)
42+
.toString()
43+
.trim();
44+
} catch {
45+
registryUrl = 'https://registry.npmjs.org/';
46+
}
47+
}
48+
return registryUrl;
49+
};
50+
})();
3351

3452
/**
3553
* Convert an npm tag to a concrete version, for example:
@@ -40,7 +58,7 @@ export async function npmResolveConcreteVersion(
4058
packageName: string,
4159
tagOrVersion: string,
4260
): Promise<string> {
43-
const url = new URL(registry);
61+
const url = new URL(getNpmRegistryUrl());
4462
url.pathname = `${packageName}/${tagOrVersion}`;
4563
const resp = await fetch(url);
4664
if (
@@ -58,11 +76,3 @@ export async function npmResolveConcreteVersion(
5876
const json: any = await resp.json();
5977
return json.version;
6078
}
61-
62-
export function getNpmRegistryUrl(): string {
63-
try {
64-
return execSync('npm config get registry').toString().trim();
65-
} catch {
66-
return 'https://registry.npmjs.org/';
67-
}
68-
}

0 commit comments

Comments
 (0)