Redis的WebPack问题

时间:2017-03-06 00:38:29

标签: node.js webpack webpack-dev-server

尝试从与插入的模块不同的Nodejs模块中从redis中检索时,我得到“模块未找到错误”:

ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'net' in E:\Code-     
Asst\node_modules\redis
 @ ./~/redis/index.js 3:10-24

ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'tls' in E:\Code-  
Asst\node_modules\redis
@ ./~/redis/index.js 4:10-24

ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'net' in E:\Code-  
Asst\node_modules\redis
@ ./~/redis/index.js 3:10-24

ERROR in ./~/redis/index.js
Module not found: Error: Cannot resolve module 'tls' in E:\Code-  
Asst\node_modules\redis
 @ ./~/redis/index.js 4:10-24

ERROR in ./~/redis-parser/lib/hiredis.js
Module not found: Error: Cannot resolve module 'hiredis' in E:\Code-  
Asst\node_modules\redis-parser\lib
@ ./~/redis-parser/lib/hiredis.js 3:14-32

我将此添加到我的webpack配置中。

node: {
console: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',  
},

现在只报告此错误。

ERROR in ./~/redis-parser/lib/hiredis.js
Module not found: Error: Cannot resolve module 'hiredis' in E:\Code-  
Asst\node_modules\redis-parser\lib
@ ./~/redis-parser/lib/hiredis.js 3:14-32

我按照http://jlongster.com/Backend-Apps-with-Webpack--Part-I的建议 现在我的webpack.config.js就是这个。

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');

var nodeModules = {};
fs.readdirSync('node_modules')
  .filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
  nodeModules[mod] = 'commonjs ' + mod;
});

module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: {
    app: [
        'webpack/hot/dev-server',
        'webpack-hot-middleware/client?reload=true',
        path.join(__dirname, 'webclient', 'clientapp.jsx')
    ]
},
target: 'node',
output: {
    path: path.join(__dirname, 'webclient', 'dist'),
    publicPath: '/dist/',
    filename: 'bundle.js'
},
externals: nodeModules,
module: {
    loaders: [
        {
            test: /\.jsx$/,
            loaders: ['babel?presets[]=react,presets[]=es2015,presets[]=stage-0']
        }, {
            test: /\.css$/,
            loader: 'style-loader!css-loader'
        }, {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loaders: ['file-loader']
        }, {

            test: /\.json$/,
            loaders: ['json-loader']

        }
        // other loader
    ]
},
resolve: {
    extensions: ['', '.js', '.jsx', '/index.js', '/index.jsx']
},
node: {
console: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',  
},
plugins: [
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
    new HtmlWebpackPlugin({template:  
    path.resolve('./webclient/index.html')})
 ]
};

我的构建和服务器启动没有错误,但在浏览器上,我得到了这个:

Uncaught ReferenceError: require is not defined

任何帮助都将受到高度赞赏 - 在过去的几天里陷入困境并陷入困境

我在package.json中的webpack依赖项是:

"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
"webpack-dev-server": "^1.16.1",
"webpack-hot-middleware": "^2.13.0"

1 个答案:

答案 0 :(得分:2)

问题是您正在尝试在浏览器中运行后端应用程序(为Node.JS制作)。 target: 'node'告诉webpack不要触及任何内置节点模块(例如fsnet,基本上所有错误),因此它们保持正常requires浏览器中不存在require is not defined错误。

从您的webpack配置看起来您正在构建一个前端应用,但您想要使用redis。这根本不可能。您不能在浏览器中拥有需要内置节点模块的模块。如果您考虑一下,它就非常有意义,例如您无法从客户端访问文件系统。

您需要做的是构建一个前端应用程序和一个后端应用程序,然后将您想要的数据传递到redis,例如使用REST API。