cli icon indicating copy to clipboard operation
cli copied to clipboard

Unable to initialize project with third-party registry that requires authentication

Open rliljest opened this issue 3 months ago • 1 comments

Environment

node: v24.7.0 OS: macOS 15.6.1

Description

When running

npx @react-native-community/cli@latest init AwesomeProject

I get the following error:

error Invalid Version: latest.
TypeError: Invalid Version: latest
    at new SemVer (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/semver/classes/semver.js:40:13)
    at compare (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/semver/functions/compare.js:5:3)
    at Object.lt (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/semver/functions/lt.js:4:29)
    at createTemplateUri (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/version.js:44:47)
    at createProject (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:354:60)
    at Object.initialize [as func] (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:434:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Command.handleAction (/Users/[user]/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/index.js:137:9)

I am in a corporate environment that has a third-party registry set up, and has explicitly blocked the standard npmjs registry. This third-party registry also requires authentication. We set this up with the npm login command, which initializes our ~/.npmrc file to look like this:

registry=https://example.com/api/npm/npm
//example.com/api/npm/npm/:_authToken=BIGAUTHTOKEN

I have isolated the issue to this line: https://github.com/react-native-community/cli/blob/ddbba6cfc5656268d39fc2d3ae9a4f58386c2c67/packages/cli/src/tools/npm.ts#L64

Essentially we assume that the registry does not require authentication, and can just fetch information from it via the URL.

Proposed solution: The _authToken config is one of the few that you cannot just extract with npm config get, due to it's sensitive nature. Instead we could add the token as an optional parameter, e.g.

npx @react-native-community/cli@latest init AwesomeProject --auth-token=BIGAUTHTOKEN

And update the fetch code to look something like this:

...
const headers = authToken ? { Authorization: `Bearer ${authToken}` } : undefined
const resp = await fetch(url, { headers });

rliljest avatar Sep 23 '25 14:09 rliljest

I get the same

This is a major blocker for starting a new project

When can this get fixed

drkhannah avatar Sep 28 '25 11:09 drkhannah