( modulePath: string, )
| 20 | * - ERR_IMPORT_MODULE_FAILED for any other import/runtime error |
| 21 | */ |
| 22 | export async function loadModule( |
| 23 | modulePath: string, |
| 24 | ): Promise<Record<string, unknown>> { |
| 25 | const moduleAbsolutePath = path.resolve(process.cwd(), modulePath); |
| 26 | |
| 27 | try { |
| 28 | const moduleUrl = pathToFileURL(moduleAbsolutePath).href; |
| 29 | const importedModule: Record<string, unknown> = await import(moduleUrl); |
| 30 | |
| 31 | return importedModule; |
| 32 | } catch (error) { |
| 33 | ensureError(error); |
| 34 | |
| 35 | if ("code" in error && error.code === "ERR_MODULE_NOT_FOUND") { |
| 36 | throw new HardhatError( |
| 37 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.MODULE_NOT_FOUND, |
| 38 | { modulePath }, |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | if (error instanceof SyntaxError) { |
| 43 | throw new HardhatError( |
| 44 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.MODULE_SYNTAX_ERROR, |
| 45 | { modulePath, errorMessage: error.message }, |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | throw new HardhatError( |
| 50 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.IMPORT_MODULE_FAILED, |
| 51 | { modulePath, errorMessage: error.message }, |
| 52 | ); |
| 53 | } |
| 54 | } |
no test coverage detected