| 110 | } |
| 111 | |
| 112 | private resolve( |
| 113 | from: string, |
| 114 | moduleName: string | undefined, |
| 115 | options: ResolveOptions = {}, |
| 116 | ): string { |
| 117 | if (moduleName == null) { |
| 118 | throw new Error( |
| 119 | 'The first argument to require.resolve must be a string. Received null or undefined.', |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | if (path.isAbsolute(moduleName)) { |
| 124 | const module = this.resolution.resolveCjsFromDirIfExists( |
| 125 | moduleName, |
| 126 | moduleName, |
| 127 | [], |
| 128 | ); |
| 129 | if (module) { |
| 130 | return module; |
| 131 | } |
| 132 | } else if (options.paths) { |
| 133 | const module = this.resolution.resolveCjsStub(from, moduleName); |
| 134 | |
| 135 | if (module) { |
| 136 | return module; |
| 137 | } |
| 138 | |
| 139 | for (const searchPath of options.paths) { |
| 140 | const absolutePath = path.resolve(from, '..', searchPath); |
| 141 | |
| 142 | // required to also resolve files without leading './' directly in the path |
| 143 | const module = this.resolution.resolveCjsFromDirIfExists( |
| 144 | absolutePath, |
| 145 | moduleName, |
| 146 | [absolutePath], |
| 147 | ); |
| 148 | |
| 149 | if (module) { |
| 150 | return module; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | throw new Resolver.ModuleNotFoundError( |
| 155 | `Cannot resolve module '${moduleName}' from paths ['${options.paths.join( |
| 156 | "', '", |
| 157 | )}'] from ${from}`, |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | try { |
| 162 | return this.resolution.resolveCjs(from, moduleName); |
| 163 | } catch (error) { |
| 164 | const module = this.resolution.getCjsMockModule(from, moduleName); |
| 165 | if (module) { |
| 166 | return module; |
| 167 | } |
| 168 | throw error; |
| 169 | } |