Skip to content

Commit f17f570

Browse files
fix: NPM Workspace throwsENOWORKSPACES error when fetching registry (#68522)
### Why? This issue occurs when a project is NPM Workspace, and has no other package managers but NPM is installed. Next.js runs [npm config get registry](https://github.com/vercel/next.js/blob/e35710f71f8e0f2844add1a97513a65a54a6f2a3/packages/next/src/lib/helpers/get-registry.ts#L13) to fetch the registry. However, running `npm config get registry` at a NPM Workspace is not allowed resulting a `ENOWORKSPACES` error. ``` $ npm config get registry npm error code ENOWORKSPACES npm error This command does not support workspaces. ``` As we didn't consume the error, it threw when the enabled `next telemetry` [triggered getVersionInfo](https://github.com/vercel/next.js/blame/fb2d2dd01a5f73ac62c4809b7b9c1490617f8705/packages/next/src/server/dev/hot-reloader-webpack.ts#L725-L728) calling `getRegistry` which threw as above. ### How? Add `--no-workspaces` flag when the pkgManager is `'npm'`. It is safe for non-workspace projects as it's equivalent to default `--workspaces=false`. Fixes #47121 Fixes NEXT-832 Fixes NDX-150 --------- Co-authored-by: Sebastian Silbermann <[email protected]>
1 parent 44b85cb commit f17f570

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

packages/next/src/lib/helpers/get-registry.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@ import { getFormattedNodeOptionsWithoutInspect } from '../../server/lib/utils'
88
* @default https://registry.npmjs.org/
99
*/
1010
export function getRegistry(baseDir: string = process.cwd()) {
11+
const pkgManager = getPkgManager(baseDir)
12+
// Since `npm config` command fails in npm workspace to prevent workspace config conflicts,
13+
// add `--no-workspaces` flag to run under the context of the root project only.
14+
// Safe for non-workspace projects as it's equivalent to default `--workspaces=false`.
15+
// x-ref: https://github.com/vercel/next.js/issues/47121#issuecomment-1499044345
16+
// x-ref: https://github.com/npm/statusboard/issues/371#issue-920669998
17+
const resolvedFlags = pkgManager === 'npm' ? '--no-workspaces' : ''
1118
let registry = `https://registry.npmjs.org/`
19+
1220
try {
13-
const pkgManager = getPkgManager(baseDir)
14-
const output = execSync(`${pkgManager} config get registry`, {
15-
env: {
16-
...process.env,
17-
NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect(),
18-
},
19-
})
21+
const output = execSync(
22+
`${pkgManager} config get registry ${resolvedFlags}`,
23+
{
24+
env: {
25+
...process.env,
26+
NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect(),
27+
},
28+
}
29+
)
2030
.toString()
2131
.trim()
2232

2333
if (output.startsWith('http')) {
2434
registry = output.endsWith('/') ? output : `${output}/`
2535
}
26-
} finally {
27-
return registry
36+
} catch (err) {
37+
throw new Error(`Failed to get registry from "${pkgManager}".`, {
38+
cause: err,
39+
})
2840
}
41+
42+
return registry
2943
}

0 commit comments

Comments
 (0)