我正在从事React / Redux项目,并且一直在努力使superagent工作。
在关注了超级代理问题之后,并在我的webpack.config文件中添加了一些变通办法之后,我能够毫无错误地构建包。不幸的是,我现在在浏览器中遇到错误:
require is not defined
指向该行:module.exports = require("tty");
我相信tty是一个核心节点模块。另外,tty的需求是通过我在客户端上的require('superagent')
调用产生的。
这是我的webpack配置:
var path = require('path')
var webpack = require('webpack')
var config = {
devtool: 'cheap-module-eval-source-map',
target: 'node',
entry: [
'webpack-hot-middleware/client',
'./client/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin(
{
'process.env.NODE_ENV': '"development"',
'global.GENTLY': false
}
),
],
module: {
loaders: [
{ test: /\.json$/, loaders: ['json'], },
{ test: /\.js$/, exlude: /node_modules/, loaders: ['babel', 'eslint'], },
]
},
// build breaks on eslint without this workaround
// https://github.com/MoOx/eslint-loader/issues/23
eslint: {
emitWarning: true
},
node: {
__dirname: true,
}
}
module.exports = config;
有人知道我的问题是什么吗?我搜索了webpack和superagent问题,这似乎是最相关的:https://github.com/facebook/react-native/issues/10
[您可以尝试他们的建议here-我看到您已将他们的建议添加到Webpack配置中,但是也许可以尝试使用其浏览器版本而不是require('superagent/lib/client)
来提出他们的第一个建议。
我在我的项目中没有使用超级代理,所以我不确定为什么它没有适当的要求。我遇到了其他以类似方式运行的库的问题。我最终使用的是浏览器版本,然后使用webpack为它加上别名(因此,我不必每次在项目中都需要提供完整版本的完整路径)。
在您的webpack配置中,您可以通过执行以下操作来别名:
resolve: {
alias: {
'superagent': 'path/to/node_modules/superagent/lib/client'
}
}
然后在您的项目中,您可以照常require('superagent')
,并且webpack将在幕后正确解决它。希望这会有所帮助!
将此(从another answer here中获取)添加到我的webpack配置中,对我来说很有效:
plugins.push(new webpack.DefinePlugin({ "global.GENTLY": false }));