Webpack cant resolve TypeScript modules

JavascriptTypescriptWebpackTs Loader

Javascript Problem Overview


I build a relay small webpack and typescript demo to play with. If i run webpack with the webpack.config.js i get this error:

ERROR in ./js/app.ts
Module not found: Error: Can't resolve './MyModule' in '/Users/timo/Documents/Dev/Web/02_Tests/webpack_test/js'
 @ ./js/app.ts 3:17-38

I have no clue what the problem could be. The module export should be correct.

Folder Structure

webpack.config.js

const path = require('path');

module.exports = {
    entry: './js/app.ts',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {test: /\.ts$/, use: 'ts-loader'}
        ]
    }
};

tsconfig.json

{
  "compilerOptions": {
        "target": "es5",
        "suppressImplicitAnyIndexErrors": true,
        "strictNullChecks": false,
        "lib": [
            "es5", "es2015.core", "dom"
        ],
        "module": "commonjs",
        "moduleResolution": "node",
        "outDir": "dist"
    },
    "include": [
        "js/**/*"
    ]
}

src/app.js

import { MyModule } from './MyModule';

let mym = new MyModule();
console.log('Demo');

mym.createTool();
console.log(mym.demoTool(3,4));

src/MyModule.ts

export class MyModule {
   createTool() {
    console.log("Test 123");
  }

   demoTool(x:number ,y:number) {
    return x+y;
  }
};

src/index.html

<html>
    <head>
        <title>Demo</title>
        <base href="/">
    </head>
    <body>
        
        <script src="dist/bundle.js"></script>
    </body>
</html>

Javascript Solutions


Solution 1 - Javascript

Webpack does not look for .ts files by default. You can configure resolve.extensions to look for .ts. Don't forget to add the default values as well, otherwise most modules will break because they rely on the fact that the .js extension is automatically used.

resolve: {
    extensions: ['.ts', '.js', '.json']
}

Solution 2 - Javascript

Tried all the suggestions above and it still didn't work.

Ended up being the tiniest detail:

In webpack.js, instead of:

resolve: {
  extensions: ['.tsx', '.ts', '.js']
},

The ordering should be:

resolve: {
  extensions: ['.ts', '.tsx', '.js']
},

Don't know why this is necessary, and to be quite honest, I'm beyond caring at this point...

Solution 3 - Javascript

Leaving this here for posterity, but I had this exact same issue and my problem was that my entry path was not relative but absolute. I changed entry from:

entry: 'entry.ts'

to

entry: './entry.ts'

Solution 4 - Javascript

I'm a bit late to the party, but is better to use:

resolve: {
    extensions: ['.jsx', '.ts', '.tsx', '...']
}

... means that we'll not overwrite the default, only add to it

Solution 5 - Javascript

I'm using tsconfig instead of webpack which also compiles the submodule to a bundle (index.js).

The issue for me was that I had forgotten to compile the submodule (ie run tsc to generate index.js) before referencing it from outside.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimo PaulView Question on Stackoverflow
Solution 1 - JavascriptMichael JungoView Answer on Stackoverflow
Solution 2 - JavascriptJonathanView Answer on Stackoverflow
Solution 3 - JavascriptDana WoodmanView Answer on Stackoverflow
Solution 4 - JavascriptLucas NoetzoldView Answer on Stackoverflow
Solution 5 - JavascriptNic ScozzaroView Answer on Stackoverflow