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: Remove hardcoded npm registry url #2520

Conversation

jasonjiicloud
Copy link
Contributor

Summary:

fixes: #2519
Official npm registry url is hardcoded, the "cli" can't run successfully in a closed environment.

Following error is displayed when running npx @react-native-community/cli@latest init AwesomeProject --verbose.

error fetch failed.
TypeError: fetch failed
    at node:internal/deps/undici/undici:13178:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getTemplateVersion (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/tools/npm.js:121:16)
    at async createTemplateUri (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/version.js:53:29)
    at async createProject (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:341:23)
    at async Object.initialize [as func] (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:422:7)
    at async Command.handleAction (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/index.js:116:9)

My solution is using getNpmRegistryUrl() to get real npm registry url in user's local environment, then it can avoid using official npm registry directly.

Test Plan:

Build and run node /path/to/react-native-cli/packages/cli/build/bin.js init AwesomeProject

Checklist

  • Documentation is up to date to reflect these changes.
  • Follows commit message convention described in CONTRIBUTING.md

@github-actions github-actions bot added the bugfix label Oct 6, 2024
@jasonjiicloud jasonjiicloud changed the title fix: Remove hardcoded npm registry url #2519 fix: Remove hardcoded npm registry url Oct 6, 2024
@@ -129,7 +127,7 @@ const minorVersion = (version: string) => {
export async function getTemplateVersion(
reactNativeVersion: string,
): Promise<TemplateVersion | undefined> {
const json = await fetch(TEMPLATE_VERSIONS_URL).then(
const json = await fetch(getNpmRegistryUrl() + '@react-native-community/template').then(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd prefer leveraging here new URL() to make sure URL is always in a correct format, e.g. when getNpmRegistryUrl()'d return value without / at the end, previous would've fail.

Suggested change
const json = await fetch(getNpmRegistryUrl() + '@react-native-community/template').then(
const json = await fetch(new URL('@react-native-community/template', getNpmRegistryUrl())).then(

Copy link
Collaborator

@szymonrybczak szymonrybczak left a comment

Choose a reason for hiding this comment

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

Makes a lot of sense, thanks! 👍

@szymonrybczak
Copy link
Collaborator

cc @blakef

@blakef blakef self-requested a review October 7, 2024 10:54
Copy link
Contributor

@blakef blakef left a comment

Choose a reason for hiding this comment

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

This is a really nice change. Couple of small things before you land it:

  1. update the URL handling to use URL as @szymonrybczak suggested
  2. fix the formatting

Copy link

github-actions bot commented Feb 4, 2025

There hasn't been any activity on this pull request in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

@github-actions github-actions bot added the stale label Feb 4, 2025
@blakef blakef merged commit 59e3ae6 into react-native-community:main Feb 4, 2025
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Official npm registry url is hardcoded, the "cli" can't run successfully in a closed environment.
3 participants