diff --git a/v-next/hardhat/src/internal/cli/error-handler.ts b/v-next/hardhat/src/internal/cli/error-handler.ts index d7bba907b6..5663be25c5 100644 --- a/v-next/hardhat/src/internal/cli/error-handler.ts +++ b/v-next/hardhat/src/internal/cli/error-handler.ts @@ -71,6 +71,12 @@ export function printErrorMessages( shouldShowStackTraces: boolean = false, print: (message: string) => void = console.error, ): void { + // If Hardhat is running on CJS mode we print a special message + if (isRequireESMError(error)) { + printEsmOnlyErrorMessage(print); + return; + } + const showStackTraces = shouldShowStackTraces || getErrorWithCategory(error).category === ErrorCategory.OTHER; @@ -151,3 +157,18 @@ function getErrorMessages(error: unknown): ErrorMessages { }; } } + +function isRequireESMError(error: unknown): boolean { + return ( + error instanceof Error && + "code" in error && + error.code === "ERR_REQUIRE_CYCLE_MODULE" && + error.message.includes("Cannot require() ES Module") + ); +} + +function printEsmOnlyErrorMessage(print: (message: string) => void) { + print(`Hardhat only supports ESM projects. + +Please make sure you have \`"type": "module"\` in your package.json`); +} diff --git a/v-next/hardhat/src/internal/cli/init/init.ts b/v-next/hardhat/src/internal/cli/init/init.ts index 608b4251e7..867a057cd7 100644 --- a/v-next/hardhat/src/internal/cli/init/init.ts +++ b/v-next/hardhat/src/internal/cli/init/init.ts @@ -17,10 +17,7 @@ import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path"; import chalk from "chalk"; import { findClosestHardhatConfig } from "../../config-loading.js"; -import { - getHardhatVersion, - getLatestHardhatVersion, -} from "../../utils/package.js"; +import { getHardhatVersion } from "../../utils/package.js"; import { HARDHAT_NAME } from "./constants.js"; import { @@ -134,23 +131,24 @@ export async function printWelcomeMessage(): Promise { chalk.cyan(`👷 Welcome to ${HARDHAT_NAME} v${hardhatVersion} 👷\n`), ); - // Warn the user if they are using an outdated version of Hardhat - try { - const latestHardhatVersion = await getLatestHardhatVersion(); - if (hardhatVersion !== latestHardhatVersion) { - console.warn( - chalk.yellow.bold( - `⚠️ You are using an outdated version of Hardhat. The latest version is v${latestHardhatVersion}. Please consider upgrading to the latest version before continuing with the project initialization. ⚠️\n`, - ), - ); - } - } catch (e) { - console.warn( - chalk.yellow.bold( - `⚠️ We couldn't check if you are using the latest version of Hardhat. Please consider upgrading to the latest version if you are not using it yet. ⚠️\n`, - ), - ); - } + // TODO: Disabled this until the first release of v3 + // // Warn the user if they are using an outdated version of Hardhat + // try { + // const latestHardhatVersion = await getLatestHardhatVersion(); + // if (hardhatVersion !== latestHardhatVersion) { + // console.warn( + // chalk.yellow.bold( + // `⚠️ You are using an outdated version of Hardhat. The latest version is v${latestHardhatVersion}. Please consider upgrading to the latest version before continuing with the project initialization. ⚠️\n`, + // ), + // ); + // } + // } catch (e) { + // console.warn( + // chalk.yellow.bold( + // `⚠️ We couldn't check if you are using the latest version of Hardhat. Please consider upgrading to the latest version if you are not using it yet. ⚠️\n`, + // ), + // ); + // } } /** diff --git a/v-next/hardhat/src/internal/cli/main.ts b/v-next/hardhat/src/internal/cli/main.ts index 8a285a45a6..532eb42e1c 100644 --- a/v-next/hardhat/src/internal/cli/main.ts +++ b/v-next/hardhat/src/internal/cli/main.ts @@ -35,6 +35,7 @@ import { createHardhatRuntimeEnvironment } from "../hre-intialization.js"; import { printErrorMessages } from "./error-handler.js"; import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js"; import { getHelpString } from "./helpers/getHelpString.js"; +import { printNodeJsVersionWarningIfNecessary } from "./node-version.js"; import { ensureTelemetryConsent } from "./telemetry/telemetry-permissions.js"; import { printVersionMessage } from "./version.js"; @@ -50,6 +51,8 @@ export async function main( ): Promise { const print = options.print ?? console.log; + printNodeJsVersionWarningIfNecessary(print); + const log = debug("hardhat:core:cli:main"); let builtinGlobalOptions; @@ -176,6 +179,8 @@ export async function main( if (options.rethrowErrors) { throw error; } + + process.exitCode = 1; } } diff --git a/v-next/hardhat/src/internal/cli/node-version.ts b/v-next/hardhat/src/internal/cli/node-version.ts new file mode 100644 index 0000000000..87a21f474b --- /dev/null +++ b/v-next/hardhat/src/internal/cli/node-version.ts @@ -0,0 +1,48 @@ +// NOTE: We don't use the `semver` package because it's slow to load, and this +// is always run during the initialization of the CLI. + +import chalk from "chalk"; + +const MIN_SUPPORTED_NODE_VERSION = [22, 10, 0]; + +function isNodeVersionSupported(): boolean { + try { + const [majorStr, minorStr, patchStr] = process.versions.node.split("."); + + const major = parseInt(majorStr, 10); + const minor = parseInt(minorStr, 10); + const patch = parseInt(patchStr, 10); + + if (major < MIN_SUPPORTED_NODE_VERSION[0]) { + return false; + } + + if (minor < MIN_SUPPORTED_NODE_VERSION[1]) { + return false; + } + + if (patch < MIN_SUPPORTED_NODE_VERSION[2]) { + return false; + } + } catch { + // If our parsing of the version fails we assume it's supported. + return true; + } + + return true; +} + +export function printNodeJsVersionWarningIfNecessary( + print: (message: string) => void, +): void { + if (isNodeVersionSupported()) { + return; + } + + print(""); + print( + chalk.bold(`${chalk.yellow("WARNING:")} You are using Node.js ${process.versions.node} which is not supported by Hardhat. +Please upgrade to ${MIN_SUPPORTED_NODE_VERSION.join(".")} or a later version.`), + ); + print(""); +}