Skip to content

Commit

Permalink
rather than throw our own error, throw the error from node's ESM loader
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Feb 21, 2022
1 parent 203325b commit b0eb980
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,7 @@ export function createEsmHooks(tsNodeService: Service) {
rememberResolvedViaCommonjsFallback.add(resolution);
return { url: resolution, format: 'commonjs' };
} catch (commonjsResolverError) {
throw new Error(
`Resolution via the ECMAScript loader failed.\n` +
`ts-node guessed that this resolution was likely the entrypoint script, so attempted a fallback to the CommonJS resolver.\n` +
`CommonJS resolver threw:\n` +
`${
(commonjsResolverError as Error)?.message ??
commonjsResolverError
}`
);
throw esmResolverError;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/esm-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ test.suite('esm', (test) => {
expect(err).toBe(null);
expect(stdout.trim()).toBe('Hello world!');
});
test('fails as expected when entrypoint does not exist at all', async (t) => {
const { err, stderr } = await exec(
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./esm-loader-entrypoint-cjs-fallback/does-not-exist`
);
expect(err).toBeDefined();
expect(stderr).toContain(`Cannot find module `);
});
}
);
});
Expand Down

0 comments on commit b0eb980

Please sign in to comment.