TypeStrong / ts-loader

TypeScript loader for webpack
https://johnnyreilly.com/ts-loader-goes-webpack-5
MIT License
3.46k stars 431 forks source link

Module build failed: TypeError: Cannot read property 'afterCompile' of undefined #729

Closed James34Shaw100 closed 6 years ago

James34Shaw100 commented 6 years ago

I've just installed this module to use with Laravel's Mix that uses Webpack under the covers. Followed guide for setup here: https://sebastiandedeyne.com/posts/2017/typescript-with-laravel-mix

I'm getting a similar error to:

728

Module build failed: TypeError: Cannot read property 'afterCompile' of undefined at successfulTypeScriptInstance (/.../node_modules/ts-loader/dist/instances.js:147:27) at Object.getTypeScriptInstance (/.../node_modules/ts-loader/dist/instances.js:48:12) at Object.loader (/.../node_modules/ts-loader/dist/index.js:16:41)

johnnyreilly commented 6 years ago

It sounds like one of the things in the mix isn't webpack 4... I'll leave this open for now but I suspect this isn't a ts-loader issue.

James34Shaw100 commented 6 years ago

Tested switching from manual configuration to simpler built-in ts support in Mix support. webpack.mix.js now looks like this:

let mix = require('laravel-mix');
mix.autoload({
    'jquery': ['$', 'window.jQuery', 'jQuery'],
    'vue': ['Vue','window.Vue'] 
});
mix.webpackConfig({
    resolve: {
        alias: {
            '@': path.resolve('resources/assets/sass'),
        }
    }
});
mix.js('resources/assets/js/bootstrap.js', 'public/js').version();
mix.js('resources/assets/js/app.js', 'public/js').version();
mix.ts('resources/assets/js/admin.ts', 'public/js').version();

mix.sass('resources/assets/sass/app.scss', 'public/css').version()

mix.copyDirectory('resources/assets/images', 'public/images');
mix.copy('node_modules/trumbowyg/dist/ui/icons.svg', 'public/css');

mix.then(function () {
    require('child_process').exec('php artisan laroute:generate', null, (error, stdout, stderr) => {
        if (error) {
            throw error;
        }
        console.log(stdout);
    });
});

I also stripped down admin.ts to just:

import Vue from 'vue'
const app = new Vue({
    el: '#app'
});

to make sure there wasn't any actual errors for it to run in to. But still nothing.

griffonn commented 6 years ago

Also happens while running webpack right away:

webpack --config webpack.config.js ERROR in ./index.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined

Config:

const path = require('path');

module.exports = {
    entry: './index.ts',
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },
    output: {
        filename: 'output.js',
        path: path.resolve(__dirname, 'dist')
    }
};
johnnyreilly commented 6 years ago

Whenever you see Cannot read property 'afterCompile' of undefined it probably means ts-loader is dealing with webpack 3 not webpack 4.

johnnyreilly commented 6 years ago

Maybe install ts-loader 3.5.0 if you're not using webpack 4?

johnnyreilly commented 6 years ago

Maybe install ts-loader 3.5.0 if you're not using webpack 4?

James34Shaw100 commented 6 years ago

Can confirm it was a versioning issue. Laravel Mix is presently still on older version of webpack at time of writing. TypeScript compiler was able to run once I swapped out for the 3.5.0 version of ts-loader.

johnnyreilly commented 6 years ago

Thanks for confirming!

an5rag commented 6 years ago

Saved me from endlessly trying different options - thanks @johnnyreilly!

liu2080019 commented 6 years ago

@johnnyreilly thank u````

johnnyreilly commented 6 years ago

My pleasure!

boogie4eva commented 6 years ago

Thanks @johnnyreilly This worked for me .

sakalys commented 6 years ago

For those coming from angular 5+ world. This means that you must install ts-loader v3.x if ng -v says that webpack version is 3.x.

danurasenan commented 3 years ago

Hello,

This is not part of this question. but any one how to fix this.

ReferenceError: self is not defined
    at Object.eeMe (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:199163:4)
    at __webpack_require__ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:20:30)
    at Module.YpNe (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:176155:66)
    at __webpack_require__ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:20:30)
    at Module.UZG+ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:163034:78)
    at __webpack_require__ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:20:30)
    at Module.c9hY (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:188309:100)
    at __webpack_require__ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:20:30)
    at Module.IqRD (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:97334:82)
    at __webpack_require__ (D:\Projects\Angular\backend angular CSL\dist\multikart-backend\server\main.js:20:30)