gpt4 book ai didi

javascript - 要求未在反射元数据上定义 - __webpack_require__ 问题

转载 作者:行者123 更新时间:2023-12-03 02:07:49 27 4
gpt4 key购买 nike

我正在尝试在 Visual Studio 上启动我的 Angular 应用程序,但是当它启动时,它停留在“正在加载...”部分。

如果我阅读 Chrome 的错误控制台,我会收到以下错误:

ERROR_ON_CHROME_CONSOLE

Uncaught ReferenceError: require is not defined at Object. < anonymous > __ webpack_require __

reflect-metadata 包含以下内容: module.exports = require("reflect-metadata"); ,其中“require”导致错误。

<小时/>

这是我的一些代码...

webpack.config.js

const path = require('path');    
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
var nodeExternals = require('webpack-node-externals');

module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {

externals: [nodeExternals()], // in order to ignore all modules in node_modules folder

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$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: [ 'to-string-loader', 'style-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
{ 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 AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#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 AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},

target: 'node',
devtool: 'inline-source-map'
});

return [clientBundleConfig, serverBundleConfig];
};
<小时/>

在互联网上搜索,所有故障排除都建议在 systemjs.config 文件上执行某些操作,但我的不是 angular-cli 应用程序,所以我无法执行此操作。

<小时/>

更新部分

  1. 更新#1

看起来问题是由在浏览器模式下执行的 webpack-node-externals 引起的。

必须找到另一种方法。

<小时/>

有任何疑难解答或潜在的解决方案建议吗?

提前致谢!

<小时/>
  • 更新#2
  • 我已经成功了,请参阅下面的答案

    最佳答案

    明白了!

    该问题是由我的常用配置中使用的 webpack-node-externals 引起的。

    查看我的问题和我对自己问题的回答:Webpack - Excluding node_modules with also keep a separated browser and server management了解更多详情。

    所以,简而言之,我遵循的步骤如下:

    • 安装 requireJS ==> http://requirejs.org/docs/node.html
    • 从我的通用 webpack 配置中删除 externals: [nodeExternals()],//为了忽略 node_modules 文件夹中的所有模块并将其添加到我的服务器配置(在我的问题之前完成,但这是非常重要的一步)[请参阅此答案或下面的代码片段中链接的问题中的 webpack.config.js 内容]
    • 添加 target: 'node', 在我上面的外部点之前,在我的服务器端部分下(在我的问题之前完成,但这是一个非常重要的步骤)[参见问题中的 webpack.config.js 内容链接在这个答案或下面的代码片段中]
      这确保浏览器端保留 target:'web' (默认目标),并且 target 成为仅用于服务器的节点。
    • 从 powershell 手动启动 webpack 配置 vendor 命令 webpack --config webpack.config.vendor.js
    • 从 powershell 手动启动 webpack 配置命令 webpack --config webpack.config.js

    这对我有用!希望它也适用于阅读此问题并遇到此问题的其他人!

    <小时/>

    webpack.config.js内容:

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

    var nodeExternals = require('webpack-node-externals');

    module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
    //removed from here, moved below.
    //externals: [nodeExternals()], // in order to ignore all modules in node_modules folder

    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$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack' },
    { test: /\.html$/, use: 'html-loader?minimize=false' },
    { test: /\.css$/, use: [ 'to-string-loader', 'style-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
    { 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 AotPlugin({
    tsConfigPath: './tsconfig.json',
    entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#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 AotPlugin({
    tsConfigPath: './tsconfig.json',
    entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
    exclude: ['./**/*.browser.ts']
    })
    ]),
    output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, './ClientApp/dist')
    },

    //added target and externals HERE, in order to prevent webpack to read node_modules
    //this also prevents fake-positives parsing errors
    target: 'node',
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder,
    devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
    };

    关于javascript - 要求未在反射元数据上定义 - __webpack_require__ 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729051/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com