Skip to content

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

Closed

Description

@milenkovic

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

Activity

  1. Dobryvechir commented on Mar 11, 2017

    @Dobryvechir

    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.

  2. PaybackMan commented on Mar 16, 2017

    @PaybackMan

    horrible error message

  3. maffelbaffel commented on Mar 22, 2017

    @maffelbaffel

    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 )
    
  4. arlindonatal commented on Mar 22, 2017

    @arlindonatal

    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" />
    
  5. colltoaction commented on Mar 25, 2017

    @colltoaction

    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));
        }
    };
  6. DanWahlin commented on Mar 26, 2017

    @DanWahlin

    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. :-)

  7. colltoaction commented on Mar 26, 2017

    @colltoaction
  8. tavelli commented on Apr 12, 2017

    @tavelli

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

  9. colltoaction commented on Apr 13, 2017

    @colltoaction

    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
            }),
        // ....
  10. tavelli commented on Apr 13, 2017

    @tavelli

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

  11. hheexx commented on Apr 13, 2017

    @hheexx

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

  12. bkdotcom commented on Apr 14, 2017

    @bkdotcom

    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>
    
  13. colltoaction commented on Apr 14, 2017

    @colltoaction
  14. mariussl commented on Apr 19, 2017

    @mariussl

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

  15. rbarcelos commented on Apr 24, 2017

    @rbarcelos

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

  16. hheexx commented on Apr 25, 2017

    @hheexx

    Deployed in 1.3.1

  17. mariussl commented on Apr 26, 2017

    @mariussl

    thx @hheexx . bugfix confirmed.

  18. filipesilva commented on May 23, 2017

    @filipesilva
    Contributor

    Fixed by #6063

  19. uscbsitric commented on Nov 3, 2017

    @uscbsitric

    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.

  20. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    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.

  21. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs: investigationRequires some digging to determine if action is needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions