( 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 ( |
| 36 | "code" in error && |
| 37 | (error.code === "ERR_MODULE_NOT_FOUND" || |
| 38 | // Loading a directory is valid, but if it doesn't have an index file, |
| 39 | // you get this error |
| 40 | error.code === "ERR_UNSUPPORTED_DIR_IMPORT") |
| 41 | ) { |
| 42 | throw new HardhatError( |
| 43 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.MODULE_NOT_FOUND, |
| 44 | { modulePath }, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | if (error instanceof SyntaxError) { |
| 49 | throw new HardhatError( |
| 50 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.MODULE_SYNTAX_ERROR, |
| 51 | { modulePath, errorMessage: error.message }, |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | throw new HardhatError( |
| 56 | HardhatError.ERRORS.HARDHAT_VERIFY.VALIDATION.IMPORT_MODULE_FAILED, |
| 57 | { modulePath, errorMessage: error.message }, |
| 58 | ); |
| 59 | } |
| 60 | } |
no test coverage detected