Repository: nodejs/node
File: lib/internal/modules/cjs/loader.js (JavaScript)

Code before:
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    const typeFromPjson = pkg?.data.type;
    if (typeFromPjson === 'module' || typeFromPjson === 'commonjs' || !typeFromPjson) {
      format = typeFromPjson;
    }
  } else if (StringPrototypeEndsWith(filename, '.mts') && tsEnabled) {
    format = 'module-typescript';
  } else if (StringPrototypeEndsWith(filename, '.cts') && tsEnabled) {
    format = 'commonjs-typescript';
  } else if (StringPrototypeEndsWith(filename, '.ts') && tsEnabled) {
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    const typeFromPjson = pkg?.data.type;
    if (typeFromPjson === 'module') {
      format = 'module-typescript';
    } else if (typeFromPjson === 'commonjs') {
      format = 'commonjs-typescript';
    } else {
      format = 'typescript';
    }
  } else if (path.extname(filename) === '') {
    // Extensionless files skip the .js suffix check above. When type: commonjs
    // is explicit, force commonjs format so ESM syntax surfaces as SyntaxError
    // instead of silently delegating to ESM.
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    if (pkg?.data?.type === 'commonjs') {
      format = 'commonjs';
    }
  }
  const { source, format: loadedFormat } = loadSource(module, filename, format);
  // Function require shouldn't be used in ES modules when require(esm) is disabled.
  if ((loadedFormat === 'module' || loadedFormat === 'module-typescript') &&
    !getOptionValue('--require-module')) {
    const err = getRequireESMError(module, pkg, source, filename);
    throw err;
  }
  module._compile(source, filename, loadedFormat);
};

/**
 * Native handler for `.json` files.
 * @param {Module} module The module to compile
 * @param {string} filename The file path of the module
 */
Module._extensions['.json'] = function(module, filename) {
  const { source: content } = loadSource(module, filename, 'json');

  try {
    setOwnProperty(module, 'exports', JSONParse(stripBOM(content)));
  } catch (err) {
    err.message = filename + ': ' + err.message;
    throw err;

Review comment (suggestion):
N/B: I think we should also allow passing the explicit "module" type field in package.json here.

Code after:
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    const typeFromPjson = pkg?.data.type;
    if (typeFromPjson === 'module' || typeFromPjson === 'commonjs' || !typeFromPjson) {
      format = typeFromPjson;
    }
  } else if (StringPrototypeEndsWith(filename, '.mts') && tsEnabled) {
    format = 'module-typescript';
  } else if (StringPrototypeEndsWith(filename, '.cts') && tsEnabled) {
    format = 'commonjs-typescript';
  } else if (StringPrototypeEndsWith(filename, '.ts') && tsEnabled) {
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    const typeFromPjson = pkg?.data.type;
    if (typeFromPjson === 'module') {
      format = 'module-typescript';
    } else if (typeFromPjson === 'commonjs') {
      format = 'commonjs-typescript';
    } else {
      format = 'typescript';
    }
  } else if (path.extname(filename) === '') {
    // Extensionless files skip the .js suffix check above. When type is explicit,
    // follow it so ESM syntax surfaces as SyntaxError for commonjs instead of
    // silently delegating to ESM.
    pkg = packageJsonReader.getNearestParentPackageJSON(filename);
    const typeFromPjson = pkg?.data?.type;
    if (typeFromPjson === 'commonjs' || typeFromPjson === 'module') {
      format = typeFromPjson;
    }
  }
  const { source, format: loadedFormat } = loadSource(module, filename, format);
  // Function require shouldn't be used in ES modules when require(esm) is disabled.
  if ((loadedFormat === 'module' || loadedFormat === 'module-typescript') &&
    !getOptionValue('--require-module')) {
    const err = getRequireESMError(module, pkg, source, filename);
    throw err;
  }
  module._compile(source, filename, loadedFormat);
};

/**
 * Native handler for `.json` files.
 * @param {Module} module The module to compile
 * @param {string} filename The file path of the module
 */
Module._extensions['.json'] = function(module, filename) {
  const { source: content } = loadSource(module, filename, 'json');

  try {
    setOwnProperty(module, 'exports', JSONParse(stripBOM(content)));
  } catch (err) {
    err.message = filename + ': ' + err.message;