Skip to content

ERROR in Cannot read property 'getSourceFile' of undefined #5329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
milenkovic opened this issue Mar 9, 2017 · 20 comments
Closed

ERROR in Cannot read property 'getSourceFile' of undefined #5329

milenkovic opened this issue Mar 9, 2017 · 20 comments
Labels
needs: investigation Requires some digging to determine if action is needed

Comments

@milenkovic
Copy link

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

@Dobryvechir
Copy link

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.

@PaybackMan
Copy link

horrible error message

@filipesilva filipesilva added the needs: investigation Requires some digging to determine if action is needed label Mar 17, 2017
@maffelbaffel
Copy link

maffelbaffel commented Mar 22, 2017

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 )

@arlindonatal
Copy link

arlindonatal commented Mar 22, 2017

I have the same problem. After changing this method, the error persists:

ERROR in Cannot read property 'getSourceFile' of undefined


ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/utils/hmac-http-client.ts (39,63): Cannot find name 'CryptoJS'.)
/home/arlindo/git/vendedoronline/frontend/src/app/utils/hmac-http-client.ts (41,63): Cannot find name 'CryptoJS'.)
/home/arlindo/git/vendedoronline/frontend/src/app/utils/hmac-http-client.ts (43,63): Cannot find name 'CryptoJS'.)

ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/security/securityToken.ts (5,9): Cannot find name '_'.)

ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/account/account.ts (12,13): Cannot find name '_'.)

ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/empresa/empresa.ts (11,13): Cannot find name '_'.)

ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/fabricante/fabricante.ts (10,13): Cannot find name '_'.)

ERROR in /home/arlindo/git/vendedoronline/frontend/src/app/linha/linha.ts (10,13): Cannot find name '_'.)

The references:

///<reference path="../../../typings/cryptojs/cryptojs.d.ts" />
///<reference path="../../../typings/lodash/lodash.d.ts" />

@colltoaction
Copy link

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));
    }
};

@DanWahlin
Copy link

Thanks for sharing @tinchou! That fixed it for me. Was having the same problem on Windows 10....wasted more time than I care to admit on this issue. :-)

@colltoaction
Copy link

colltoaction commented Mar 26, 2017 via email

@tavelli
Copy link

tavelli commented Apr 12, 2017

@tinchou can you share plugins context of your webpack config for this workaround?

@colltoaction
Copy link

What do you mean? This?

    plugins: [
        aotPlugin,
        new webpack.optimize.UglifyJsPlugin({
            mangle: { screw_ie8: true },
            compress: {
                screw_ie8: true,
                warnings: false,
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true,
            debug: false
        }),
    // ....

@tavelli
Copy link

tavelli commented Apr 13, 2017

@tinchou yea thanks, i had dumb mistake with var aotPlugin = new ngToolsWebpack.AotPlugin({ part defined after webpack config instead of before

@hheexx
Copy link

hheexx commented Apr 13, 2017

@tinchou screw_ie8: true, is default. You don't need that.

@bkdotcom
Copy link

bkdotcom commented Apr 14, 2017

where is this webpack.config.js file everyone speaks of along with the aotPlugin settings?
Does @angular/cli use it?
I've been trying to get a --prod and/or --aot build to work for months... seem to be in upgrade/dependency hell / along with not having any idea where the error lines

ng build --aot

No NgModule metadata found for 'AppModule'

expanding that:

ZoneAwareError http://localhost:4200/polyfills.bundle.js:7954:21
NgModuleResolver.prototype.resolve http://localhost:4200/vendor.bundle.js:36464:23
CompileMetadataResolver.prototype.getNgModuleMetadata http://localhost:4200/vendor.bundle.js:37043:37
JitCompiler.prototype._loadModules http://localhost:4200/vendor.bundle.js:48238:41
JitCompiler.prototype._compileModuleAndComponents http://localhost:4200/vendor.bundle.js:48197:47
JitCompiler.prototype.compileModuleAsync http://localhost:4200/vendor.bundle.js:48159:16
PlatformRef_.prototype._bootstrapModuleWithZone http://localhost:4200/vendor.bundle.js:6792:16
PlatformRef_.prototype.bootstrapModule http://localhost:4200/vendor.bundle.js:6778:16
[104] http://localhost:4200/main.bundle.js:57:1
__webpack_require__ http://localhost:4200/inline.bundle.js:53:12
[265] http://localhost:4200/main.bundle.js:100:18
__webpack_require__ http://localhost:4200/inline.bundle.js:53:12
webpackJsonpCallback http://localhost:4200/inline.bundle.js:24:23
<anonymous>

@colltoaction
Copy link

colltoaction commented Apr 14, 2017 via email

@mariussl
Copy link

thx @tinchou for the workaround, works perfectly for me (win7x64)

@rbarcelos
Copy link

rbarcelos commented Apr 24, 2017

Any ETA for when a final fix for this will be deployed? Thanks

@hheexx
Copy link

hheexx commented Apr 25, 2017

Deployed in 1.3.1

@mariussl
Copy link

thx @hheexx . bugfix confirmed.

@filipesilva
Copy link
Contributor

Fixed by #6063

@uscbsitric
Copy link

I solved min by pressing Ctrl + C on the terminal to end the current npm start,
then ran the entire setup with a npm start.

This error occurred to me when I was switching branches.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: investigation Requires some digging to determine if action is needed
Projects
None yet
Development

No branches or pull requests