我正在做一个React/Redux项目,并且一直在努力让superagent正常工作。
在跟踪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
发布于 2015-12-23 02:25:36
您可以尝试他们建议的here --我看到您已经将他们建议的内容添加到您的webpack配置中,但也许可以尝试他们的第一个建议,使用他们的浏览器版本而不是require('superagent/lib/client)。
我没有在我的项目中使用superagent,所以我不确定为什么它不能正确地要求。然而,我也遇到过其他库以类似方式运行的问题。我最终使用了一个浏览器版本,然后用webpack给它起了别名(这样我就不必每次在项目中需要构建版本时都记得给出它的完整路径)。
在您的webpack配置中,您可以通过执行以下操作来实现别名:
resolve: {
alias: {
'superagent': 'path/to/node_modules/superagent/lib/client'
}
}然后在你的项目中,你可以像往常一样使用require('superagent'),webpack会在幕后妥善解决它。希望这能有所帮助!
发布于 2019-11-16 20:03:38
将这段代码(摘自another answer here)添加到我的webpack配置中,帮了我大忙:
plugins.push(new webpack.DefinePlugin({ "global.GENTLY": false }));https://stackoverflow.com/questions/34406431
复制相似问题