Solve - Module not found: Can't resolve 'ts-loader' error


Borislav Hadzhiev

Mon Mar 21 20222 min read

Solve - Module not found: Can't resolve 'ts-loader' error #

To solve the error "Module not found: Error: Can't resolve 'ts-loader'", make sure to install the ts-loader package by opening your terminal in your project's root directory and running the command npm install -D ts-loader typescript and restart your development server.

module not found cant resolve ts loader

Open your terminal in your project's root directory (where your package.json file is located) and run the following commands:

# 👇️ with NPM npm install --save-dev ts-loader typescript # ---------------------------------------------- # 👇️ with YARN yarn add ts-loader typescript --dev

The command will add the ts-loader package to the development dependencies of your project.

Make sure to add the .ts and .tsx extensions to the extensions array in your webpack config file.

module.exports = { // ... rest resolve: { // 👇️ Add `.ts` and `.tsx` as a resolvable extension. extensions: [".ts", ".tsx", ".js"] }, module: { rules: [ // 👇️ all files with a `.ts` or `.tsx` extension will be handled by `ts-loader` { test: /\.tsx?$/, loader: "ts-loader" } ] } };

For other configuration examples refer to the npm page of the ts-loader package.

Make sure to restart your development server and your IDE if necessary. Your dev server won't pick up the changes until you stop it and re-run the npm start command.

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

# 👇️ delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json # 👇️ clean npm cache npm cache clean --force npm install
Make sure to restart your IDE and dev server if the error still persists. VSCode often glitches and a reboot solves things sometimes.

If you're still getting the "Module not found: Error: Can't resolve 'ts-loader'" error, open your package.json file and make sure it contains the ts-loader package in the devDependencies object.

{ // ... rest "devDependencies": { "ts-loader": "^9.2.8", "typescript": "^4.6.3", }, }

The ts-loader module should NOT be globally installed or be in your project's dependencies, it should be in the devDependencies object in your package.json file.

You can try to manually add the lines and re-run npm install.

npm install

Or install the latest version of the package:

npm install --save-dev ts-loader@latest typescript@latest
Use the search field on my Home Page to filter through my more than 1,000 articles.