Hello,
I use mysql2 module in nodejs with serverless framework, and I get the following issue :
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'fromPacket' of undefined",
"stack": [
"TypeError: Cannot read property 'fromPacket' of undefined",
" at a._fatalError [as handshakeInit] (/var/task/webpack:/node_modules/mysql2/lib/commands/client_handshake.js:89:18)",
" at a.this [as execute] (/var/task/webpack:/node_modules/mysql2/lib/commands/command.js:33:9)",
" at T.handlePacket (/var/task/webpack:/node_modules/mysql2/lib/connection.js:403:9)",
" at s._handleNetworkError [as onPacket] (/var/task/webpack:/node_modules/mysql2/lib/connection.js:67:34)",
" at sequenceId [as executeStart] (/var/task/webpack:/node_modules/mysql2/lib/packet_parser.js:70:15)",
" at Socket.this (/var/task/webpack:/node_modules/mysql2/lib/connection.js:74:29)",
" at Socket.emit (events.js:198:13)",
" at addChunk (_stream_readable.js:288:12)",
" at readableAddChunk (_stream_readable.js:269:11)",
" at Socket.Readable.push (_stream_readable.js:224:10)"
]
}
To "solve" this issue, webpack minification must be disable : set "mode" to "developpment" in webpack.config.js.
Is there a way to make it compatible with webpack minification ?
I'm having the same issue. Changing the mode does work, but not ideal.
linking related: https://github.com/sidorares/node-mysql2/pull/992#issuecomment-538838598
@cain004 what version are you using? v1.7.0 removed some dynamic requires to be more compatible with bundlers
also @cain004 @JeremieDemarchez if you can prepare simple self-contained way to get same error for me that would be really helpful
@sidorares using v1.7.0. Below is a sample project with some brief notes in the handler:
https://github.com/cain004/mysql2-service
Try using _keep_fnames_ in webpack config:
const TerserPlugin = require('terser-webpack-plugin');
[...]
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
keep_fnames: true
}
})
]
},
Try using _keep_fnames_ in webpack config:
const TerserPlugin = require('terser-webpack-plugin'); [...] optimization: { minimizer: [ new TerserPlugin({ terserOptions: { keep_fnames: true } }) ] },
it's working
@cain004 can you try what @azulinternet suggested? If it works for you I'll close this issue
using webpack-node-externals does the trick.
webpack.config.js
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
devtool: slsw.lib.webpack.isLocal ? 'cheap-module-eval-source-map' : 'source-map',
resolve: {
extensions: ['.mjs', '.json', '.ts'],
symlinks: false,
cacheWithContext: false,
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,
loader: 'ts-loader',
exclude: [
[
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, '.serverless'),
path.resolve(__dirname, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
plugins: [
// new ForkTsCheckerWebpackPlugin({
// eslint: true,
// eslintOptions: {
// cache: true
// }
// })
],
};
Using nodeExternals is a workaround not a fix. It simply tells webpack not to bundle mysql2.
If I use nodeExternals then my Lambda does not include the required dependencies. I've tried setting:
minimize: false
mode: 'development'
Still I get this error. I'm not using serverless but CDK to create my infrastructure.
Most helpful comment
it's working