-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include a less cryptic error message when trying to require an ES mod…
…ule (#2264)
- Loading branch information
1 parent
aae711c
commit d599dfe
Showing
5 changed files
with
53 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Provides a try guarded require call that will throw a more detailed error when | ||
* the ERR_REQUIRE_ESM error code is encountered. | ||
* | ||
* @param {string} path File path to require from. | ||
*/ | ||
export default function tryRequire(path: string) { | ||
try { | ||
return require(path) | ||
} catch (error) { | ||
if (error.code === 'ERR_REQUIRE_ESM') { | ||
throw Error( | ||
`Cucumber expected a CommonJS module at '${path}' but found an ES module. | ||
Either change the file to CommonJS syntax or use the --import directive instead of --require. | ||
Original error message: ${error.message}` | ||
) | ||
} else { | ||
throw error | ||
} | ||
} | ||
} |