Skip to content

Commit

Permalink
fix: NPM Workspace throwsENOWORKSPACES error when fetching registry (
Browse files Browse the repository at this point in the history
…#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]>
  • Loading branch information
devjiwonchoi and eps1lon authored Aug 7, 2024
1 parent 44b85cb commit f17f570
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 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,36 @@ 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
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) {
throw new Error(`Failed to get registry from "${pkgManager}".`, {
cause: err,
})
}

return registry
}

0 comments on commit f17f570

Please sign in to comment.