Doctor Strange
Doctor Strange

Reputation: 209

Publish No NgModule metadata found for 'AppModule'

I'm trying to publish my app, When first trying I got into all sorts of issues.. So I updated all the packages to the latest versions (angular 5, etc), made some changes to webpack, As people said you should change the aot plugin.

Now I ran into this weird issue where in Development version without publish I can get my server to work. (dotnet run xxx.dll) But when I publish and try to run I always run into this error when entering the website:

Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: No NgModule metadata found for 'AppModule'.
      Error: No NgModule metadata found for 'AppModule'.
          at NgModuleResolver.module.exports.NgModuleResolver.resolve (/var/TestApp/ClientApp/dist/vendor.js:61057:23)
          at CompileMetadataResolver.module.exports.CompileMetadataResolver.getNgModuleMetadata (/var/TestApp/ClientApp/dist/vendor.js:56047:60)
          at JitCompiler.module.exports.JitCompiler._loadModules (/var/TestApp/ClientApp/dist/vendor.js:74490:87)
          at JitCompiler.module.exports.JitCompiler._compileModuleAndComponents (/var/TestApp/ClientApp/dist/vendor.js:74451:36)
          at JitCompiler.module.exports.JitCompiler.compileModuleAsync (/var/TestApp/ClientApp/dist/vendor.js:74367:37)
          at CompilerImpl.module.exports.CompilerImpl.compileModuleAsync (/var/TestApp/ClientApp/dist/vendor.js:98089:49)
          at PlatformRef.module.exports.PlatformRef.bootstrapModule (/var/TestApp/ClientApp/dist/vendor.js:16396:25)
          at /var/TestApp/ClientApp/dist/main-server.js:21258:111
          at /var/TestApp/ClientApp/dist/vendor.js:86229:35
          at /var/TestApp/ClientApp/dist/vendor.js:86348:33
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: No NgModule metadata found for 'AppModule'.
Error: No NgModule metadata found for 'AppModule'.
    at NgModuleResolver.module.exports.NgModuleResolver.resolve (/var/TestApp/ClientApp/dist/vendor.js:61057:23)
    at CompileMetadataResolver.module.exports.CompileMetadataResolver.getNgModuleMetadata (/var/TestApp/ClientApp/dist/vendor.js:56047:60)
    at JitCompiler.module.exports.JitCompiler._loadModules (/var/TestApp/ClientApp/dist/vendor.js:74490:87)
    at JitCompiler.module.exports.JitCompiler._compileModuleAndComponents (/var/TestApp/ClientApp/dist/vendor.js:74451:36)
    at JitCompiler.module.exports.JitCompiler.compileModuleAsync (/var/TestApp/ClientApp/dist/vendor.js:74367:37)
    at CompilerImpl.module.exports.CompilerImpl.compileModuleAsync (/var/TestApp/ClientApp/dist/vendor.js:98089:49)
    at PlatformRef.module.exports.PlatformRef.bootstrapModule (/var/TestApp/ClientApp/dist/vendor.js:16396:25)
    at /var/TestApp/ClientApp/dist/main-server.js:21258:111
    at /var/TestApp/ClientApp/dist/vendor.js:86229:35
    at /var/TestApp/ClientApp/dist/vendor.js:86348:33
   at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.<InvokeExportAsync>d__7`1.MoveNext()

I suspect it might be related to my webpack config or Development/Production setting (I updated to angular 5 + changed webpack config AOTPlugin to AngularCompilerPlugin

my webpack config:

const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: [ '.js', '.ts' ] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /\.ts|\.tsx$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize', 'less-loader'] },
                { test: /\.less$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize', 'less-loader'] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
            // Plugins that apply in production builds only
            new webpack.optimize.UglifyJsPlugin(),
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
                exclude: ['./**/*.server.ts']
            })
        ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

Can someone point me in the right direction why this is happening?

Upvotes: 1

Views: 867

Answers (1)

Daniel Pucher
Daniel Pucher

Reputation: 199

The Server Side Rendering causes the problem as this works in a different way in Angular 5 and does not work with the current ASP.NET Core template.

https://github.com/aspnet/JavaScriptServices/issues/1385

Disabling Server Side Rendering can be done by simply removing the asp-prerender-module tag helper in the Views\Home\Index.cshtml file.

See also: No NgModule metadata found for 'AppModule' Error in Angular 5 server-side rendering with --env.prod

Upvotes: 1

Related Questions