Webpack TypeScript Resolution Plugin
resolve-typescript-plugin is a webpack plugin designed to bridge the gap between TypeScript's ES Module output expectations and webpack's module resolution for ES Modules. It allows developers to import TypeScript files using a `.js` extension (e.g., `import { foo } from './bar.js'`) which aligns with native ES Module resolution behavior in Node.js and browsers, rather than webpack's default of requiring `.ts` or no extension. The current stable version is 2.0.1. Its release cadence has been infrequent since the introduction of an equivalent built-in feature in webpack. A key differentiator was solving the specific import extension problem for ESM TypeScript projects before webpack v5.74.0; now, its primary use case is for projects still on older webpack versions that require this specific resolution behavior. It is effectively in maintenance mode for older webpack versions, as webpack itself now offers `resolve.extensionAlias` for this functionality, making the plugin largely obsolete for modern webpack setups.
Common errors
-
Module not found: Error: Can't resolve './some-module.js' in '...' (webpack)
cause Attempting to import a TypeScript file using a `.js` extension without the plugin or `extensionAlias` configured correctly, or `resolve.extensions` still contains `.ts`/`.tsx`.fixEnsure `new ResolveTypeScriptPlugin()` is correctly added to `resolve.plugins` in your webpack config. Also, remove `.ts` and `.tsx` from your `resolve.extensions` array to avoid conflicts. For webpack >= 5.74.0, use `resolve.extensionAlias` instead of the plugin. -
ERROR in Error: webpack.WebpackError: TypeError: The 'this' context cannot be converted to an object. (webpack)
cause This error can occur in older versions of the plugin or webpack if the `webpack.config.js` is set to CommonJS and the plugin is imported incorrectly, or there's a misconfiguration of webpack's ESM handling.fixEnsure your webpack config aligns with the project's module type. If `"type": "module"` is in `package.json`, use `import` for the plugin and ensure the webpack config itself is an ESM module (e.g., `export default { ... }`). For CommonJS configs, use `const ResolveTypeScriptPlugin = require('resolve-typescript-plugin');`.
Warnings
- deprecated This plugin is largely obsolete for webpack v5.74.0 and newer. Webpack now provides equivalent functionality via `resolve.extensionAlias`.
- breaking Node.js versions 12 and 17 are no longer supported since v2.0.0.
- gotcha Previous versions of the README recommended setting `resolve.fullySpecified` to `true`. This is no longer recommended as it can break compatibility with `webpack-dev-server` and other tooling.
- gotcha When using webpack 4.x with ES modules, your `webpack.config.js` file cannot be in ES module format if `"type": "module"` is set in `package.json`.
- gotcha The plugin by default does not resolve TypeScript files within `node_modules`. This can be unexpected if you have TS-only dependencies that require this resolution behavior.
Install
-
npm install resolve-typescript-plugin -
yarn add resolve-typescript-plugin -
pnpm add resolve-typescript-plugin
Imports
- ResolveTypeScriptPlugin
const ResolveTypeScriptPlugin = require('resolve-typescript-plugin');import ResolveTypeScriptPlugin from 'resolve-typescript-plugin';
- ResolveTypeScriptPlugin Options
new ResolveTypeScriptPlugin({ includeNodeModules: true });
Quickstart
import ResolveTypeScriptPlugin from 'resolve-typescript-plugin';
/** @type {import('webpack').Configuration} */
export default {
mode: 'development',
entry: './src/index.ts',
output: {
filename: 'bundle.js',
path: new URL('./dist', import.meta.url).pathname
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
// It's recommended to remove .ts and .tsx from resolve.extensions when using this plugin
extensions: ['.js', '.json'],
plugins: [new ResolveTypeScriptPlugin()]
},
// Recommended for ES Module projects
experiments: {
outputModule: true
}
};