Repository: nodejs/node
File: lib/internal/modules/esm/module_job.js (JavaScript)

Code before:
const CJSGlobalLike = [
  'require',
  'module',
  'exports',
  '__filename',
  '__dirname',
];
const getUndefinedCJSGlobalLike = (errorMessage) => {
  for (const globalLike of CJSGlobalLike) {
    if (errorMessage === `${globalLike} is not defined`) {
      return globalLike;
    }
  }
  return null;
};


/**
 *
 * @param {Error} e
 * @param {string} url
 * @returns {void}
 */
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
  const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
  if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {

    if (hasTopLevelAwait) {
      let advice = '';
      if (undefinedGlobal === 'require') {
        advice = 'replace require() with import';
      } else if (undefinedGlobal === 'module' || undefinedGlobal === 'exports') {
        advice = 'use export instead of module.exports/exports';
      } else if (undefinedGlobal === '__filename') {
        advice = 'use import.meta.filename instead';
      } else if (undefinedGlobal === '__dirname') {
        advice = 'use import.meta.dirname instead';
      }

      e.message = `Cannot determine intended module format because both ${undefinedGlobal} and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, ${advice}.`;
      e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
      return;
    }

    e.message += ' in ES module scope';

    if (StringPrototypeStartsWith(e.message, 'require ')) {
      e.message += ', you can use import instead';
    }

    const packageConfig =

Review comment (suggestion):
```suggestion
  const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
  if (notDefinedGlobalLike) {
```

Code after:
const {
  ERR_REQUIRE_ASYNC_MODULE,
} = require('internal/errors').codes;
let hasPausedEntry = false;

const CJSGlobalLike = [
  'require',
  'module',
  'exports',
  '__filename',
  '__dirname',
];
const findCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
  ArrayPrototypeFind(
    CJSGlobalLike,
    (globalLike) => errorMessage === `${globalLike} is not defined`,
  );


/**
 *
 * @param {Error} e
 * @param {string} url
 * @returns {void}
 */
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
  const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);

  if (notDefinedGlobalLike) {
    if (hasTopLevelAwait) {
      let advice;
      switch (notDefinedGlobalLike) {
        case 'require':
          advice = 'replace require() with import';
          break;
        case 'module':
        case 'exports':
          advice = 'use export instead of module.exports/exports';
          break;
        case '__filename':
          advice = 'use import.meta.filename instead';
          break;
        case '__dirname':
          advice = 'use import.meta.dirname instead';
          break;
      }

      e.message = `Cannot determine intended module format because both '${notDefinedGlobalLike}' and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, ${advice}.`;
      e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
      return;
    }