node.js - Redis 的 WebPack 问题

标签 node.js webpack webpack-dev-server

在尝试从与插入的 Nodejs 模块不同的 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"

最佳答案

问题是您正在尝试在浏览器中运行后端应用程序(为 Node.JS 制作)。 target: 'node' 告诉 webpack 不要触及任何内置的 Node 模块(例如 fsnet,基本上所有你遇到的错误得到),因此它们仍然是浏览器中不存在的常规 requires ,并且您会收到 require is not Defined 错误。

从您的 webpack 配置来看,您似乎正在构建一个前端应用程序,但您不知何故想要使用 redis。那根本不可能。您不能在浏览器中拥有需要内置 Node 模块的模块。如果您仔细想想,这是完全有道理的,例如您无权从客户端访问文件系统。

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

关于node.js - Redis 的 WebPack 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42615860/

相关文章:

Javascript 查找字符串并用变量值替换

node.js - 在 node.js 中找不到 common.gypi 错误

angular - 在 webpack.config.js 中包含加载程序时出错

javascript - Webpack - 如何通过实时重新加载将 .html 片段包含到 index.html 中?

javascript - "export ' 默认 ' (imported as ' __vue_script__ ') was not found in ' !!babel-loader

node.js - 带有 NGINX proxy_pass 的 Webpack 开发服务器

node.js - NodeJS - Mongoose(对象没有方法 "id")

node.js - 无服务器部署 : Recoverable error occurred (write ECONNRESET), 休眠约 5 秒。尝试 4 个中的 1 个

javascript - Webpack:未捕获的 ReferenceError:未定义 vendor_bd98b4ed288c6b156cc9

javascript - webpack build less files 输出一个 css minify 文件