Issue
Upon upgrading angular-cli from 22.1 to 25.5 (installing angular-cli@latest) and angular from version 2.2.3 to 2.3.1, the following problem occurred:
ERROR in ./src/app/app.module.ts
Module build failed: TypeError: Cannot read property 'text' of undefined
at IdentifierObject.TokenOrIdentifierObject.getText (~/projects/MyProject/node_modules/typescript/lib/typescript.js:53644:56)
at ~/projects/MyProject/node_modules/@ngtools/webpack/src/loader.js:83:72
at Array.some (native)
at ~/projects/MyProject/node_modules/@ngtools/webpack/src/loader.js:83:32
at Array.filter (native)
at _removeModuleId (~/projects/MyProject/node_modules/@ngtools/webpack/src/loader.js:82:10)
at ~/projects/MyProject/node_modules/@ngtools/webpack/src/loader.js:167:48
@ ./src/main.ts 5:0-45
@ multi main
I am unable to determine the root cause of this issue as the main.ts
seems normal:
import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
Two other files are also failing with the same error message - first the environment.ts
with nothing unusual and second a sub module which failed with the added snippet:
@ ./src async
@ ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js
@ ./~/@angular/core/src/linker.js
@ ./~/@angular/core/src/core.js
@ ./~/@angular/core/index.js
@ ./src/main.ts
@ multi main
NodeModules SourceCode
The referenced section typescript.js:53644:56
appears as follows:
[53643] TokenOrIdentifierObject.prototype.getText = function (sourceFile) {
[53644] return (sourceFile || this.getSourceFile()).text.substring(this.getStart(), this.getEnd());
[53645] };
and loader.js:83:72
& 83:32
points to webpack and this code block:
[078] exports.removeModuleIdOnlyForTesting = removeModuleIdOnlyForTesting;
[...] function _removeModuleId(refactor) {
[...] var sourceFile = refactor.sourceFile;
[...] refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
[...] .filter(function (node) {
[083] return node.properties.some(function (prop) { return prop.name.getText() == 'moduleId'; });
[...] })
[...] .forEach(function (node) {
[...] var moduleIdProp = node.properties.filter(function (prop, idx) {
[...] return prop.name.getText() == 'moduleId';
[...] })[0];
[...] // get the trailing comma
[...] var moduleIdCommaProp = moduleIdProp.parent.getChildAt(1).getChildren()[1];
[...] refactor.removeNodes(moduleIdProp, moduleIdCommaProp);
[...] });
[093] }
and loader.js:167:48
[148] // Super simple TS transpiler loader for testing / isolated usage. does not type check!
[...] function ngcLoader(source) {
[...] this.cacheable();
[...] var cb = this.async();
[...] var sourceFileName = this.resourcePath;
[...] var plugin = this._compilation._ngToolsWebpackPluginInstance;
[...] // We must verify that AotPlugin is an instance of the right class.
[...] if (plugin && plugin instanceof plugin_1.AotPlugin) {
[...] var refactor_2 = new refactor_1.TypeScriptFileRefactor(sourceFileName, plugin.compilerHost, plugin.program);
[...] Promise.resolve()
[...] .then(function () {
[...] if (!plugin.skipCodeGeneration) {
[...] return Promise.resolve()
[...] .then(function () { return _removeDecorators(refactor_2); })
[...] .then(function () { return _replaceBootstrap(plugin, refactor_2); });
[...] }
[...] else {
[...] return Promise.resolve()
[...] .then(function () { return _replaceResources(refactor_2); })
[167] .then(function () { return _removeModuleId(refactor_2); });
[...] }
[169] })
[...] ...
Inquiry
Has anyone encountered this issue before or can provide insights into why webpack(?) may be failing - or can elaborate on what the compiler is attempting to do and why it's encountering errors?
Update
angular-cli.json
:
{
"project": {
"version": "1.0.0-beta.25.5",
"name": "myproject"
},
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"prefix": "app",
"mobile": false,
"styles": [
"assets/css/main.css"
],
"scripts": [
"assets/lib/stomp.min.js"
],
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"live": "environments/environment.live.ts"
}
}
],
"addons": [],
"packages": [],
"e2e": {
"protractor": {
"config": "./protractor.conf.js"
}
},
"test": {
"karma": {
"config": "./karma.conf.js"
}
},
"defaults": {
"styleExt": "css",
"prefixInterfaces": false,
"inline": {
"style": false,
"template": false
},
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
}
tsconfig.json
:
{
"compilerOptions": {
"baseUrl": "",
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es6",
"dom"
],
"mapRoot": "./",
"module": "es6",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
]
},
"files": [
"typings/index.d.ts"
]
}