-
Notifications
You must be signed in to change notification settings - Fork 27.4k
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
Module not found: Fully Specified ESM Imports (with .js
extension) in TypeScript
#41961
Comments
Workaround 2 works, but it has a typo. webpackConfig.resolve.extensionAlias = {
'.js': ['.ts', '.tsx', '.js'],
- '.mjs': ['.mts', '.js'],
+ '.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs'],
}; I think webpackConfig.resolve.extensionAlias = {
'.js': ['.ts', '.tsx', '.jsx', '.js'],
'.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs'],
}; |
Edited, thanks. I did mention that Workaround 2 worked also in the description above. It would still be nice if this was zero-config for people though 👍 |
Yes, but I believe this fixes the error you mentioned. :)
Definitely! |
Indeed, missed this - it is working! I've updated my description above. |
With this /** @type {import('next').NextConfig} */
export default {
webpack: (webpackConfig, { webpack }) => {
webpackConfig.resolve.extensionAlias = {
".js": [".ts", ".tsx", ".js", ".jsx"],
".mjs": [".mts", ".mjs"],
".cjs": [".cts", ".cjs"],
};
return webpackConfig;
},
}; You run into a build error if you deep import import withGraphQLReact from "next-graphql-react/withGraphQLReact.mjs";
Can the workaround be tweaked somehow to account for this? |
Not sure exactly why that's happening, but you could try reporting that over in webpack/webpack#13252, since that's the related issue in the webpack repo. If you find a solution, let us know and I'll update my post above. Or you could try Workaround 2, which uses |
This workaround appears to work: // @ts-check
import { existsSync } from "node:fs";
import { join, parse, resolve } from "node:path";
/** @type {import("next").NextConfig} */
export default {
typescript: {
// TODO: Investigate why TypeScript via Next.js builds has ESM/CJS default
// import interop errors when vanilla TypeScript via VS Code and the package
// script `type-check` is ok.
ignoreBuildErrors: true,
},
webpack: (webpackConfig, { webpack }) => {
// TODO: Remove this config once this Next.js issue that `.tsx` files can’t
// be imported using the `.js` file extension is fixed:
// https://github.com/vercel/next.js/issues/41961
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
webpackConfig.plugins.push(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
new webpack.NormalModuleReplacementPlugin(/\.js$/, function (
/** @type {{ context: string, request: string }} */
resource
) {
// Skip a non relative import (e.g. a bare import specifier).
if (resource.request.startsWith(".")) {
const path = resolve(resource.context, resource.request);
if (
// Skip the relative import if it reaches into `node_modules`.
!path.includes("node_modules") &&
!existsSync(path)
) {
const { dir, name } = parse(path);
const extensionlessPath = join(dir, name);
for (const fallbackExtension of [".tsx", ".ts", ".js"]) {
if (existsSync(extensionlessPath + fallbackExtension)) {
resource.request = resource.request.replace(
/\.js$/,
fallbackExtension
);
break;
}
}
}
}
})
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return webpackConfig;
},
}; |
## ESM: support module option for tsconfig.json - fixes #37525 - fixes #41961 With [TypeScript 4.7 providing ECMAScript Module Support](https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#ecmascript-module-support-in-node-js), we can now set this in our tsconfig.json file for the [module](https://www.typescriptlang.org/tsconfig#module) option. Webpack added "extensionAlias" to solve importing ts files with .js extension -> webpack/enhanced-resolve#351 Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Hi there, first of all, thanks so much for the hard work on Next.js 🙌 13 is looking great!
Importing a file using the fully-specified ESM-style imports with
.js
leads to aModule not found
error:pages/index.tsx
components/Component.tsx
:Error message:
This does not match the behavior of the TypeScript compiler, which will resolve these files. See microsoft/TypeScript#41887 (comment) for more details.
experimental.fullySpecified config option
This also happens while using the
experimental.fullySpecified: true
option in the Next.js config:next.config.mjs
Workaround 1:
resolve.extensionAlias
configuration optionwebpack does have a
resolve.extensionAlias
configuration as of5.74.0
:But this should be zero-config.
Related issue in webpack: webpack/webpack#13252
Workaround 2:
webpack.NormalModuleReplacementPlugin
configuration optionConfigure the
NormalModuleReplacementPlugin
inside your webpack confignext.config.mjs
Also, this should be zero-config.
Expected Behavior
Fully-specified ESM-style imports with
.js
extensions should resolve to the respective TypeScript files out of the box (as TSC does it), without any further webpack / Turbopack configuration necessary.Link to reproduction
https://stackblitz.com/edit/vercel-next-js-y6faeb?file=components%2FComponent.tsx,pages%2Findex.tsx,next.config.js
To Reproduce
.js
The text was updated successfully, but these errors were encountered: