Node.js Redis的网页包问题
在尝试从不同于插入的Nodejs模块的redis中检索时,我得到了“未找到模块错误”: 我把它添加到我的网页配置中Node.js Redis的网页包问题,node.js,webpack,webpack-dev-server,Node.js,Webpack,Webpack Dev Server,在尝试从不同于插入的Nodejs模块的redis中检索时,我得到了“未找到模块错误”: 我把它添加到我的网页配置中 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-
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
我听从了他的建议
现在我的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": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
"webpack-dev-server": "^1.16.1",
"webpack-hot-middleware": "^2.13.0"
问题是您试图在浏览器中运行后端应用程序(为Node.JS制作)。
目标:“节点”
告诉webpack不要触摸任何内置节点模块(例如fs
或net
,基本上是您遇到的所有错误),因此它们保持常规requires
,浏览器中不存在该模块,并且您会得到require未定义的错误
从你的网页配置来看,你似乎正在构建一个前端应用程序,但不知何故你想使用redis
。这根本不可能。浏览器中不能有需要内置节点模块的模块。如果您仔细想想,它是非常有意义的,例如,您没有从客户端访问文件系统的权限
您需要做的是构建一个前端和一个后端应用程序,然后在redis
中传输您想要的数据,例如使用REST API。谢谢Jungo勋爵!一段时间以来,我一直在尝试使用webpack将自己的节点库编译成一个web包。我遇到了很多问题。OP遇到的问题是最后一个。但是,直到你提到现在显而易见的原因,我才意识到这一点。
"webpack": "^1.13.2",
"webpack-dev-middleware": "^1.8.4",
"webpack-dev-server": "^1.16.1",
"webpack-hot-middleware": "^2.13.0"