Skip to content

Commit add4cf8

Browse files
authored
fix: surface ts-node compile errors (#5572)
* fix: surface ts-node compile errors Prevents "Cannot find module" when there are TS errors * fix: remove ts-node error condition check below debug logging
1 parent 9cc3ada commit add4cf8

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

lib/nodejs/esm-utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ const requireModule = async (file, esmDecorator) => {
9898
return require(file);
9999
} catch (requireErr) {
100100
debug("requireModule caught err: %O", requireErr.message);
101+
if (requireErr.name === 'TSError') {
102+
throw requireErr;
103+
}
101104
try {
102105
return dealWithExports(await formattedImport(file, esmDecorator));
103106
} catch (importErr) {

test/node-unit/esm-utils.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ describe("esm-utils", function () {
3434
},
3535
);
3636
});
37+
38+
it("should surface the ts-node TSError error rather than falling back to `import(...)`", async function () {
39+
return expect(
40+
() =>
41+
esmUtils.requireOrImport(
42+
"../../test/node-unit/fixtures/mock-ts-node-compile-err.ts",
43+
),
44+
"to be rejected with error satisfying",
45+
{
46+
name: "TSError",
47+
message: /A TS compilation error/,
48+
},
49+
);
50+
});
3751
});
3852

3953
describe("loadFilesAsync()", function () {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Stripped down version of this error from ts-node:
3+
https://github.com/TypeStrong/ts-node/blob/ddb05ef23be92a90c3ecac5a0220435c65ebbd2a/src/index.ts#L435
4+
It's thrown when ts-node hits a compilation error.
5+
*/
6+
class TSError extends Error {
7+
constructor(...args) {
8+
super(...args);
9+
10+
this.name = 'TSError';
11+
}
12+
}
13+
throw new TSError('A TS compilation error');

0 commit comments

Comments
 (0)