Repository: facebook/react
File: scripts/devtools/publish-release.js (JavaScript)

Code before:
  const {otp} = await inquirer.prompt([
    {
      type: 'input',
      name: 'otp',
      message: 'Please provide an NPM two-factor auth token:',
    },
  ]);

  console.log('');

  if (!otp) {
    console.error(chalk.red(`Invalid OTP provided: "${chalk.bold(otp)}"`));
    process.exit(0);
  }

  for (let index = 0; index < NPM_PACKAGES.length; index++) {
    const npmPackage = NPM_PACKAGES[index];
    const packagePath = join(ROOT_PATH, 'packages', npmPackage);
    const {version} = readJsonSync(join(packagePath, 'package.json'));

    // Check if this package version has already been published.
    // If so we might be resuming from a previous run.
    // We could infer this by comparing the build-info.json,
    // But for now the easiest way is just to ask if this is expected.
    const info = await execRead(`npm view ${npmPackage}@${version}`)
      // For npm v8+ (node 16+) view command responds with 404 code, instead of 200 with empty response
      .catch(childProcessError => {
        if (childProcessError.stderr.startsWith('npm ERR! code E404')) {
          return null;
        }

        throw childProcessError;
      });

    if (info) {
      console.log('');
      console.log(
        `${npmPackage} version ${chalk.bold(
          version
        )} has already been published.`
      );

      await confirm(`Is this expected (will skip ${npmPackage}@${version})?`);
      continue;
    }

    if (DRY_RUN) {
      console.log(`Publishing package ${chalk.bold(npmPackage)}`);
      console.log(chalk.dim(`  npm publish --otp=${otp}`));
    } else {
      const publishPromise = exec(`npm publish --otp=${otp}`, {

Review comment (suggestion):
I tested with node v16.14.2 (npm v8.5.0) it still gives empty response instead of error.
You don't have to find the exact version tho. You can just say something like "early versions of npm view gives empty response, but older versions give 404 error. Catch the error to keep it consistent."

Code after:
  const {otp} = await inquirer.prompt([
    {
      type: 'input',
      name: 'otp',
      message: 'Please provide an NPM two-factor auth token:',
    },
  ]);

  console.log('');

  if (!otp) {
    console.error(chalk.red(`Invalid OTP provided: "${chalk.bold(otp)}"`));
    process.exit(0);
  }

  for (let index = 0; index < NPM_PACKAGES.length; index++) {
    const npmPackage = NPM_PACKAGES[index];
    const packagePath = join(ROOT_PATH, 'packages', npmPackage);
    const {version} = readJsonSync(join(packagePath, 'package.json'));

    // Check if this package version has already been published.
    // If so we might be resuming from a previous run.
    // We could infer this by comparing the build-info.json,
    // But for now the easiest way is just to ask if this is expected.
    const info = await execRead(`npm view ${npmPackage}@${version}`)
      // Early versions of npm view gives empty response, but newer versions give 404 error.
      // Catch the error to keep it consistent.
      .catch(childProcessError => {
        if (childProcessError.stderr.startsWith('npm ERR! code E404')) {
          return null;
        }

        throw childProcessError;
      });

    if (info) {
      console.log('');
      console.log(
        `${npmPackage} version ${chalk.bold(
          version
        )} has already been published.`
      );

      await confirm(`Is this expected (will skip ${npmPackage}@${version})?`);
      continue;
    }

    if (DRY_RUN) {
      console.log(`Publishing package ${chalk.bold(npmPackage)}`);
      console.log(chalk.dim(`  npm publish --otp=${otp}`));
    } else {