发布没有为“AppModule”找到的NgModule元数据'

时间:2017-11-19 13:27:21

标签: angular webpack asp.net-core

我试图发布我的应用程序,第一次尝试时我遇到了各种各样的问题..所以我将所有软件包更新到最新版本(角度5等),对webpack进行了一些更改,As人们说你应该改变aot插件。

现在我遇到了这个奇怪的问题,在没有发布的开发版本中我可以让我的服务器工作。 (dotnet运行xxx.dll) 但是当我发布并尝试运行时,我总是在进入网站时遇到此错误:

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()

我怀疑它可能与我的webpack配置或开发/生产设置有关(我更新为angular 5 +将webpack配置AOTPlugin更改为AngularCompilerPlugin

我的webpack配置:

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

有人能指出我正确的方向,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

服务器端呈现导致问题,因为它在Angular 5中以不同的方式工作,并且不适用于当前的ASP.NET Core模板。

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

只需删除asp-prerender-module文件中的Views\Home\Index.cshtml代码帮助程序即可停用服务器端呈现。

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