我正在使用Node / React / Webpack构建应用程序。我想将照片存储在Google Cloud Storage服务器上,但是当我按照文档进行操作时,出现了webpack错误
Uncaught ReferenceError: require is not defined
at eval (storage":1)
at Object.@google-cloud/storage (index_bundle.js:5038)
我认为这可能是加载程序的问题,浏览器正在读取针对Node的代码,但是我对webpack不太熟悉。因此,我很难对此进行故障排除。
下面是我的webpack.config.js文件
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'index_bundle.js',
publicPath: '/'
},
module: {
rules: [
{
test: /\.jsx?$/,
use: [{
loader: 'babel-loader',
}],
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader',
}),
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.(jpe?g|png|gif)$/i,
use: {
loader: 'file-loader',
}
}
]
},
externals: {
'@google-cloud/storage': 'commonjs @google-cloud/storage'
},
devServer: {
historyApiFallback: true,
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
}),
new ExtractTextPlugin('css/[name].css', {
allChunks: true,
}),
]
}