Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NPM Workspace throwsENOWORKSPACES error when fetching registry #68522

Merged
merged 8 commits into from
Aug 7, 2024
34 changes: 25 additions & 9 deletions packages/next/src/lib/helpers/get-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@ import { getFormattedNodeOptionsWithoutInspect } from '../../server/lib/utils'
* @default https://registry.npmjs.org/
*/
export function getRegistry(baseDir: string = process.cwd()) {
const pkgManager = getPkgManager(baseDir)
// Since `npm config` command fails in npm workspace to prevent workspace config conflicts,
// add `--no-workspaces` flag to run under the context of the root project only.
// Safe for non-workspace projects as it's equivalent to default `--workspaces=false`.
// x-ref: https://github.com/vercel/next.js/issues/47121#issuecomment-1499044345
// x-ref: https://github.com/npm/statusboard/issues/371#issue-920669998
Comment on lines +12 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New implementation has way more confidence given the explainer. This is valueable context, thank you!

const resolvedFlags = pkgManager === 'npm' ? '--no-workspaces' : ''
let registry = `https://registry.npmjs.org/`

try {
const pkgManager = getPkgManager(baseDir)
const output = execSync(`${pkgManager} config get registry`, {
env: {
...process.env,
NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect(),
},
})
const output = execSync(
`${pkgManager} config get registry ${resolvedFlags}`,
{
env: {
...process.env,
NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect(),
},
}
)
.toString()
.trim()

if (output.startsWith('http')) {
registry = output.endsWith('/') ? output : `${output}/`
}
} finally {
return registry
} catch (err) {
if (err instanceof Error) {
devjiwonchoi marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Failed to get registry from "${pkgManager}".`, {
cause: err,
})
}
}

return registry
}
Loading