-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.js extension import raises MODULE_NOT_FOUND error #77
Comments
use 'foo' instead 'foo.js'. although 'foo' is 'foo.ts'. but use need as 'foo'. to try please. |
foo.js is what TypeScript node16 moduleResolution expects (for importing from a .ta file). Also in Node.js you can attach the .js extension in any version and it behaves the same way as if you did not attach the extension. Thus, to me it seems like esbuild-register should be able to follow this behavior. |
I am experiencing the same issue in a pure ESM package, trying to use esbuild-register/loader to run mocha for my TS files with JS extensions. |
Im experiencing the same issue with both I had a similar issue trying to get swc/jest to work some time ago swc-project/jest#64, that worked eventually with the solution provided in the comments there, that however was specific to jest. I'm still wondering if there's some way the authors of esbuild-register and swc-node have this working in ESM mode which I simply didn't notice yet, since using them on .ts files and then using tsc for typechecking seems to be the by far most common usecase I guess? |
Having the exact same use case, @justinhelmer. Using // .mocharc.cjs
module.exports = {
extension: ['ts'],
'node-option': ['loader=esbuild-register/loader'],
require: [
'esbuild-register',
],
} Moving to // .mocharc.cjs
module.exports = {
extension: ['ts'],
'node-option': ['experimental-specifier-resolution=node', 'loader=ts-node/esm'],
} |
In my project, I only have ".ts" files that I import as ".js". Based on this, I made a very dumb patch for esbuild-register, and applied it with diff --git a/dist/node.js b/dist/node.js
index e117be6904a8145923dce5a75b35dfe174666861..12e7be4aba3359fcd71098c85618ae618a98e076 100644
--- a/dist/node.js
+++ b/dist/node.js
@@ -4793,10 +4793,26 @@ function registerTsconfigPaths() {
const found = matchPath(request);
if (found) {
const modifiedArguments = [found, ...[].slice.call(arguments, 1)];
- return originalResolveFilename.apply(this, modifiedArguments);
+ try {
+ return originalResolveFilename.apply(this, modifiedArguments);
+ } catch (error) {
+ if (error.code === 'MODULE_NOT_FOUND' && found.endsWith('.js')) {
+ modifiedArguments[0] = found.replace(/\.js$/, '.ts');
+ return originalResolveFilename.apply(this, modifiedArguments);
+ }
+ throw error;
+ }
+ }
+ }
+ try {
+ return originalResolveFilename.apply(this, arguments);
+ } catch (error) {
+ if (error.code === 'MODULE_NOT_FOUND' && arguments[0].endsWith('.js')) {
+ arguments[0] = arguments[0].replace(/\.js$/, '.ts');
+ return originalResolveFilename.apply(this, arguments);
}
+ throw error;
}
- return originalResolveFilename.apply(this, arguments);
};
return () => {
Module._resolveFilename = originalResolveFilename; There is probably a smarter version of this that handles the other typical file suffixes, avoids try/catch, etc. |
See the following reproduction: https://codesandbox.io/s/esbuild-register-js-module-not-found-reproduction-gyogmj
run
yarn start
I am pretty sure this should be allowed.
The text was updated successfully, but these errors were encountered: