我正在使用reactjs,redux,webpack和相关库进行chrome扩展。 除了googleapis之外,每个图书馆都运作完美, 当我使用googleapis将文件保存到驱动器中时不起作用。
我尝试安装其他一些节点模块,它们运行良好,但是只有googleapis抛出诸如require not define之类的错误。
代码就像跟随
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const TOKEN_PATH = 'token.json';
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), listFiles);
});
,等等,从快速入门开始 而我的webpack就像
devtool: 'eval-cheap-module-source-map',
entry: {
todoapp: [customPath, hotScript, path.join(__dirname, '../chrome/extension/todoapp')],
background: [customPath, hotScript, path.join(__dirname, '../chrome/extension/background')],
},
devMiddleware: {
publicPath: `http://${host}:${port}/js`,
stats: {
colors: true
},
noInfo: true,
headers: { 'Access-Control-Allow-Origin': '*' }
},
hotMiddleware: {
path: '/js/__webpack_hmr'
},
output: {
path: path.join(__dirname, '../dev/js'),
filename: '[name].bundle.js',
chunkFilename: '[id].chunk.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.IgnorePlugin(/[^/]+\/[\S]+.prod$/),
new webpack.DefinePlugin({
__HOST__: `'${host}'`,
__PORT__: port,
'process.env': {
NODE_ENV: JSON.stringify('development')
}
}),
new webpack.ProvidePlugin({ // Added by Me
$: "jquery",
jQuery: "jquery"
})
],
resolve: {
extensions: ['*', '.js']
},
module: {
rules: [{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: ['react-hmre']
}
}, {
test: /\.css$/,
oneOf: [{
resourceQuery: /^\?raw$/,
use: [
'style-loader',
'css-loader'
]
}, {
use: [
'style-loader',
'css-loader?modules&sourceMap&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
{
loader: 'postcss-loader',
options: {
plugins: () => [autoprefixer]
}
}
]
}]
}]
},
node: {
net: 'empty',
tls: 'empty',
child_process: 'empty',
__filename: true,
__dirname: true
}
它在编译时抛出错误-
ERROR in ./chrome/extension/background.js
Module not found: Error: Can't resolve 'fs' in 'D:\CHROME_EXTENTION\mysavedanswers\chrome\extension'
@ ./chrome/extension/background.js 106:13-26
@ multi ./webpack/customPublicPath webpack-hot-middleware/client?path=__webpack_hmr&dynamicPublicPath=true ./chrome/extension/background
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'fs' in 'D:\CHROME_EXTENTION\mysavedanswers\node_modules\google-auth-library\build\src\auth'
@ ./node_modules/google-auth-library/build/src/auth/googleauth.js 19:11-24
@ ./node_modules/google-auth-library/build/src/index.js
@ ./node_modules/googleapis/build/src/googleapis.js
@ ./node_modules/googleapis/build/src/index.js
@ ./chrome/extension/background.js
@ multi ./webpack/customPublicPath webpack-hot-middleware/client?path=__webpack_hmr&dynamicPublicPath=true ./chrome/extension/background