Skip to content

COREPACK_NPM_REGISTRY with trailing slash causes double slash in package URLs #759

@ThePlenkov

Description

@ThePlenkov

Problem

When setting COREPACK_NPM_REGISTRY from npm get registry, if the registry URL ends with a trailing slash, Corepack incorrectly constructs package URLs with a double slash, resulting in HTTP 404 errors.

Steps to Reproduce

  1. Configure npm registry with trailing slash:
npm config set registry https://registry.example.com/artifactory/api/npm/npm/
  1. Export the registry to Corepack:
export COREPACK_NPM_REGISTRY=$(npm get registry)
  1. Try to use Corepack with yarn:
corepack use yarn

Actual Error

Internal Error: Server answered with HTTP 404 when performing the request to https://registry.example.com/artifactory/api/npm/npm//@yarnpkg/cli-dist; for troubleshooting help, see https://github.com/nodejs/corepack#troubleshooting

    at fetch (/home/user/.local/share/fnm/node-versions/v24.10.0/installation/lib/node_modules/corepack/dist/lib/corepack.cjs:22051:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async fetchAsJson (/home/user/.local/share/fnm/node-versions/v24.10.0/installation/lib/node_modules/corepack/dist/lib/corepack.cjs:22058:20)
    at async fetchAvailableVersions (/home/user/.local/share/fnm/node-versions/v24.10.0/installation/lib/node_modules/corepack/dist/lib/corepack.cjs:22001:20)
    at async fetchAvailableVersions2 (/home/user/.local/share/fnm/node-versions/v24.10.0/installation/lib/node_modules/corepack/dist/lib/corepack.cjs:22139:14)

Current Behavior

Corepack attempts to fetch packages from:

https://registry.example.com/artifactory/api/npm/npm//@yarnpkg/cli-dist

Note the double slash: npm//@yarnpkg which causes a 404 error.

Expected Behavior

Corepack should normalize the URL and fetch from:

https://registry.example.com/artifactory/api/npm/npm/@yarnpkg/cli-dist

Root Cause

The issue occurs because:

  • npm get registry returns the registry URL with a trailing slash (e.g., https://registry.example.com/artifactory/api/npm/npm/)
  • Corepack concatenates this with package paths starting with / (e.g., /@yarnpkg/cli-dist)
  • This results in a double slash: npm//@yarnpkg, which is an invalid URL path

Proposed Solution

Corepack should normalize the registry URL by:

  1. Trimming trailing slashes from COREPACK_NPM_REGISTRY before constructing package URLs, or
  2. Using proper URL joining logic (e.g., new URL(packagePath, registryUrl)) to avoid double slashes

Workaround

Manually remove the trailing slash when setting the environment variable:

export COREPACK_NPM_REGISTRY=$(npm get registry | sed 's|/$||')

Then run:

corepack use yarn

Environment

  • Node.js version: v24.10.0
  • Corepack version: bundled with Node.js v24.10.0
  • OS: Linux (WSL2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions