AOT build fails in --prod but works "almost" in --prod
- [x] bug report
- [ ] feature request
Windows 10 all latest updates.
node --version
v6.10.3
npm --version
3.10.10
ng "--version"
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.0.4
node: 6.10.3
os: win32 x64
@angular/animations: 4.1.3
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/platform-server: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.0.4
@angular/compiler-cli: 4.1.3
Repro steps.
ng build --prod
ERROR in Child compilation failed:
Module build failed: TypeError: Cannot read property 'newLine' of undefined
at Object.getNewLineCharacter (C:\...\node_modules\typescript\lib\typescript.js:9581:20)
at Object.createCompilerHost (C:\...\node_modules\typescript\lib\typescript.js:66675:26)
at Object.ngcLoader (C:\...\node_modules\@ngtools\webpack\src\loader.js:386:33):
TypeError: Cannot read property 'newLine' of undefined
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\...\src'
@ ./src/main.ts 2:0-74
@ multi ./src/main.ts
BUT
npm run ng build -- --aot=true --environment=prod --output-hashing=all --sourcemaps=false --extract-css=true
Works fine! Based on docs for build command:
--prod also sets the following non-flaggable settings:
- Adds service worker if configured in .angular-cli.json.
- Replaces process.env.NODE_ENV in modules with the production value (this is needed for some libraries, like react).
- Runs UglifyJS on the code.
So I suspect uglify? Or what can be the reason?
What I've already tried:
- Reinstall node (latest 7.* and 6*)
- Get a clean repo from scratch
- Delete node_modules
- Try on a different machine.
- Install UglifyJS globally
- Install different version of "@angular/cli" - 1.0.4 and 1.1.0-rc.0 - same result.
I've also made some investigation on what happens in loader.js and why compilerOptions are undefined:
function ngcLoader() {
...
const sourceFileName = this.resourcePath; //here is index.ts file, I'll show it later
const plugin = this._compilation._ngToolsWebpackPluginInstance; //plugin === **undefined** here! _compilation is fine, but it does not contain _ngToolsWebpackPluginInstance member.
if (plugin && plugin instanceof plugin_1.AotPlugin) { //and of course this is false
...
} else {
const options = loaderUtils.getOptions(this) || {}; //and options are {} here...
...
const compilerHost = ts.createCompilerHost(compilerOptions); //and of course it breaks here
...
}
}
And the file on which it fails is index.ts:
export * from './GJLog.component';
Also Included my npm -ls.
Any suggestions on what should be my next steps?
If you can setup a simple repro, I can take a look.
How can I setup a simple repro? It's probably something inside my current project and if I knew what's the cause probably I could resolve it myself or write a more simple bug report... Maybe some steps you can suggest to localize my issue that will lead to a simple repro?
The only thing that comes to mind is some problem with typescript versions, I think I've seen past errors similar to TypeError: Cannot read property 'newLine' of undefined being caused by that. Your npm ls shows two versions. Try changing it so you only have one.
Updated to 1.1.0-rc.1
C:\...\FolWebAdmin>npm ls typescript
[email protected] C:\...\FolWebAdmin
`-- [email protected]
So now only one version of typescript is there...
The error is still there...
Hash: 016265276845733f1ee8
Time: 36017ms
chunk {0} polyfills.1eb029c9d22e696778f3.bundle.js (polyfills) 158 kB {5} [initial] [rendered]
chunk {1} styles.1fd42aaeb02e0ce19094.bundle.css (styles) 652 bytes {5} [initial] [rendered]
chunk {2} scripts.7b76fc1c852573a644a0.bundle.js (scripts) 373 kB {5} [initial] [rendered]
chunk {3} main.ba1cb1c866fff37dfe0b.bundle.js (main) 421 bytes {4} [initial] [rendered]
chunk {4} vendor.ade5109238477711a97d.bundle.js (vendor) 814 kB [initial] [rendered]
chunk {5} inline.a72a576cc0d30d66a3ca.bundle.js (inline) 0 bytes [entry] [rendered]
ERROR in Child compilation failed:
Module build failed: TypeError: Cannot read property 'newLine' of undefined
at Object.getNewLineCharacter (C:\...\node_modules\typescript\lib\typescript.js:9618:20)
at Object.createCompilerHost (C:\...\node_modules\typescript\lib\typescript.js:66867:26)
at Object.ngcLoader (C:\...\node_modules\@ngtools\webpack\src\loader.js:397:33):
TypeError: Cannot read property 'newLine' of undefined
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\...\FolWebAdmin\src'
@ ./src/main.ts 2:0-74
@ multi ./src/main.ts
Actually others are having the same unresolved issues here: https://github.com/angular/angular-cli/issues/3781#issuecomment-303423262
In general: Probalby it makes sense to make errors more informative?
You said that if I could give you a repro you could look at it. How were you going to look at it? Possibly I can do the steps you planned to do, so we can move forward with issue investigation?
_ngToolsWebpackPluginInstance; - So this one in loader.js in undefined.
Why and where is this coming from? Probably if I could get why webpack plugin instance fails to be created we could localize the problem... Could you please help?
I'm sorry but without a repro to look at, there isn't much I can do. In #3781 suggested resolutions are also related to dependency management.
What I'd do with a simple repro would be to go through the dependencies declared in package.json and see if there's something odd. The problem you are experiencing doesn't happening in a new project, so that means there's a configuration that works.
If your app worked before, but doesn't now, try to figure out what happened around the time it stopped working.
In your issue description you also mention running ng build --prod versus npm run ng build, maybe try running npm run ng build -- --prod as well.
I wish I could help you more, but there just isn't enough information for me to give you more help. The only thing I can say for sure is that this class of errors has been due to problems in dependencies before.
Never tried with aot build before. Switched from webpack to angular-cli and aot build did not work initially. Ok, I'll try to set up a repro... Tried running ng build all different ways - no luck.
Our AOT-build broke recently too when updating some Angular packages. As it turns out we also had two dependencies on TypeScript.
Changing our TypeScript to 2.2.2 (same as angular-cli) made the project compile again. However on our build server it seems to still not work. Might just be caching though. Need to investigate further.
Hey, I think the documentation for what --prod does is not full. https://github.com/angular/angular-cli/wiki/build
I think it also runs cssnano if --prod is specified.
In the documentation, I did not find it. Please fix it...
In my case if cssnano is applied - then aot build fails with the error described... cssnano is 3.10.0...
npm ls cssnano
[email protected] C:\...\FolWebAdmin
`-- @angular/[email protected]
`-- [email protected]
Any advice?
Thank you!
Wooot! Found a solution and a way to reproduce! (probably)
one of my scss files had the following:
#gridContainer .none {
background-image: url("");
background-repeat: no-repeat;
}
Believe or not, I had to dig up to understanding that cssnano is run on top in production mode... How much easier it would be if it is documented.
So after changing it to
#gridContainer .none {
background-image: none;
background-repeat: no-repeat;
}
The error is finally fixed!!!
You should definitely do something with error messages...
I'm glad that you figured it out, and thank you for letting me know about the repro. So it seems that cssnano breaks with that css, and that then it gives a cryptic TS error somewhere in the loader.
That's definitely hard to figure out. I'll look into making this error better. Thanks again!
@filipesilva Thanks. The confusing error is the first issue. The second one is that I did not find anywhere in the docs that --prod parameter also runs cssnano... Would be nice to add it too... Thanks again.
Heya, I had a look at your repro and I get a very different error message out of it:
ERROR in ./src/styles.css
Module not found: Error: Can't resolve './.' in 'D:\sandbox\master-project\src'
@ ./src/styles.css 6:72-86
@ multi ./src/styles.css
ERROR in ./src/styles.css
Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './.' in 'D:\sandbox\master-project\src'
at factoryCallback (D:\work\cli\node_modules\webpack\lib\Compilation.js:260:39)
at factory (D:\work\cli\node_modules\webpack\lib\NormalModuleFactory.js:247:20)
at resolver (D:\work\cli\node_modules\webpack\lib\NormalModuleFactory.js:65:21)
I think this one is pretty straightforward really.
Information about the Cannot read property 'newLine' of undefined error can be found in https://github.com/angular/angular-cli/issues/3781#issuecomment-311330967.
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.