本文介绍了WebPack-Dev-Server 错误:未定义要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Webpack 本身工作正常,但 webpack-dev-server 不是.基本上,webpack 为我创建了 2 个构建文件,一个后端包和一个前端包.所以,我为每一个都有一个 webpack-config.js.我想用 webpack-dev-server 开发我的前端代码,你可以从下面我的前端 bundle.js 的 webpack-config 文件中看到.当我运行 web-pack-dev 服务器时,它能够找到并构建我的 front-end.js 和 index.html,但控制台中没有任何内容呈现,并且它给了我一个 "Uncaught ReferenceError: require is not defined"

Webpack itself is working fine, but the webpack-dev-server is not. Basically, webpack created 2 build files for me, a back-end bundle and a front-end bundle. So, I have a webpack-config.js for each of these. I want to develop my front-end code with webpack-dev-server, as you can see from my webpack-config file for my front-end-bundle.js below. When I run web-pack-dev server, it is able to find and build my front-end.js and index.html, but nothing renders in the console and it gives me a "Uncaught ReferenceError: require is not defined"

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

module.exports = {
entry: './browser/entry.js',
output: {
    path: './builds',
    filename: 'frontend.js'
},
plugins: [
    new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"development"'
    }),
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': '"development"'
        }
    })
],
module: {
    loaders: [
        {
            test: [/\.es6$/, /\.js$/, /\.jsx$/],
            exclude: 'node_modules',
            loader: 'babel-loader',
            query: {
                presets: ['react', 'es2015', 'stage-1']
            }
        },
        {
            test: /\.json$/,
            loader: 'json-loader'
        },
        {
            test: /\.html$/,
            loader: 'html-loader'
        },
    ]
},
resolve: {
    extensions: ['', '.js', '.es6', '.json'],
    root: '/Users/johnhenry/Desktop/GAMR/gamr/browser'
},
devServer: {
    contentBase: 'builds/dev-build'
},
target: 'node',
// externals: [nodeExternals()]
}

错误是在我的前端构建中由此触发的(它仅在开发服务器构建中,而不在非开发服务器 webpack 构建中):

The error is triggered by this in my front-end build (it is only in the dev server build, not in the non-dev-server webpack build):

function(module, exports) {

module.exports = require("url");

如果有人对此有所了解,将不胜感激

If anyone has insight into this, it would be much appreciated

推荐答案

尝试添加:

target: 'web'

到你的模块块.

这篇关于WebPack-Dev-Server 错误:未定义要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 08:39