Solvedangular cli ERROR in Cannot read property 'getSourceFile' of undefined

Hi, I am trying to configure webpack in order to work with AOT, however I am getting this error. I tried with changing versions but without success, in some cases I even got more errors.

OS?

Windows 10

Versions.

Node: v7.6.0
@ngtools/webpack: 1.2.12

Repro steps.

https://github.com/milenkovic/webpack-angular-aot
npm install
npm run project-aot

The log given by the failure.

ERROR in Cannot read property 'getSourceFile' of undefined

ERROR in C:/Workspace/webpack-aot/public/javascripts/core/projects/main.aot.ts (3,34): Cannot find module './ngfactory/public/javascripts/core/projects/app.module.ngfactory'.)

ERROR in ./public/javascripts/core/projects/main.aot.ts
Module not found: Error: Can't resolve './ngfactory/public/javascripts/core/projects/app.module.ngfactory' in 'C:\Workspace\webpack-aot\public\javascripts\core\projects'
@ ./public/javascripts/core/projects/main.aot.ts 3:0-103

21 Answers

✔️Accepted Answer

I'm on Windows 10 using @ngtools/webpack:1.3.0. I can confirm @maffelbaffel's fix works.

You can paste this into your webpack.config.js and avoid touching the node_modules folder, especially useful for automated deployment scenarios:

// workaround https://github.com/angular/angular-cli/issues/5329
var aotPlugin = new ngToolsWebpack.AotPlugin({
    tsConfigPath: "./tsconfig.aot.json",
    entryModule: path.resolve(__dirname, "./ClientApp/components/main.module#MainModule"),
});
aotPlugin._compilerHost._resolve = function(path_to_resolve) {
    path_1 = require("path");
    path_to_resolve = aotPlugin._compilerHost._normalizePath(path_to_resolve);
    if (path_to_resolve[0] == '.') {
        return aotPlugin._compilerHost._normalizePath(path_1.join(aotPlugin._compilerHost.getCurrentDirectory(), path_to_resolve));
    }
    else if (path_to_resolve[0] == '/' || path_to_resolve.match(/^\w:\//)) {
        return path_to_resolve;
    }
    else {
        return aotPlugin._compilerHost._normalizePath(path_1.join(aotPlugin._compilerHost._basePath, path_to_resolve));
    }
};

Other Answers:

The error lies in node_modules@ngtools\webpack\src\compiler_host.js in function _resolve(path), when the operating system is Windows.
In some cases, this function calls to path.join, which returns a normalized path under the current OS, which does not correspond to the idea of _resolve function to replace backslashes with normal slashes.
Until a new version with this fix appears, you can replace manually that function with the code as follows:
_resolve(path) {
path = this._normalizePath(path);
if (path[0] == '.') {
return this._normalizePath(path_1.join(this.getCurrentDirectory(), path));
}
else if (path[0] == '/' || path.match(/^\w://)) {
return path;
}
else {
return this._normalizePath(path_1.join(this._basePath, path));
}
}
After this, the problem disappears.

horrible error message

This error literally cost me 5 hours debugging. Replacing with

_resolve(path) {
        path = this._normalizePath(path);
        if (path[0] == '.') {
            return this._normalizePath(path_1.join(this.getCurrentDirectory(), path));
        }
        else if (path[0] == '/' || path.match(/^\w:\//)) {
            return path;
        }
        else {
            return this._normalizePath(path_1.join(this._basePath, path));
        }
    }

solved it for me thought.

@Dobryvechir's code was missing a backslash:

        else if (path[0] == '/' || path.match(/^\w://)) {
                                                     ^
SyntaxError: Unexpected token )

More Issues: