'Webpack production bundle gives error, development bundle works fine
While developing node api, I have a problem when building a production bundle with webpack. In webpack config, if mode is set to 'development', everything works fine. If mode is set to 'production', running node bundle.js, gives me error which does not say much. What could be the problem or how should I debug it.
Error
at c.<anonymous> (/Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:747025)
at c.<anonymous> (/Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:217821)
at c._callback (/Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:217609)
at c.l.end (/Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:434863)
at /Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:255978
at Array.forEach (<anonymous>)
at /Users/sten/Sites/Projects/Farmi/API/New/dist/bundle.js:2:255955
at processTicksAndRejections (node:internal/process/task_queues:78:11)
My webpack production config:
const path = require('path');
module.exports = {
mode: 'production',
target: 'node',
entry: './src/index.js',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: 'babel-loader',
},
],
},
output: {
path: path.resolve(__dirname, 'dist/'),
filename: 'bundle.js',
},
};
package.json:
{
"name": "API",
"version": "1.0.0",
"description": "API",
"main": "index.js",
"author": "API",
"license": "MIT",
"dependencies": {
"@koa/router": "^10.1.1",
"axios": "^0.27.2",
"file-extension": "^4.0.5",
"get-4byte-chars": "^0.1.0",
"koa": "^2.13.4",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"mysql": "^2.18.1",
"pm3": "^0.0.0",
"unique-filename": "^1.1.1",
"winston": "^3.7.2"
},
"devDependencies": {
"@babel/core": "^7.17.9",
"@babel/plugin-transform-runtime": "^7.17.10",
"@babel/preset-env": "^7.16.11",
"babel-loader": "^8.2.5",
"nodemon-webpack-plugin": "^4.7.1",
"webpack": "^5.72.1",
"webpack-cli": "^4.9.2"
},
"scripts": {
"dev": "webpack --config=webpack.config.dev.js --watch",
"build": "webpack --config=webpack.config.prod.js",
"start": "node ./dist/bundle.js"
}
}
Solution 1:[1]
Found the problem by eliminating blocks of code. Looks like webpack breaks Node's MySQL package while minifying.
Here's a reference:
https://github.com/serverless/serverless/issues/5946
Removed the minification as suggested and it works.
To get the error message from mysql I had to wrap the code inside promise into try / catch block, even tough I'm rejecting the promise with err and that should be caught by the try / catch block calling the function _dbQuery ... or not ?
Original version:
const _dbQuery = async (queryString, values = null) => {
return new Promise((resolve, reject) => {
const connection = mysql.createConnection({
...configuration.mysql,
});
connection.connect((err) => {
if (err) reject(new Error(err.sqlMessage));
});
if (values !== null) {
connection.query(queryString, values, (err, res, fields) => {
if (err) reject(new Error(err.sqlMessage));
resolve(res);
connection.end();
});
} else {
connection.query(queryString, (err, res, fields) => {
if (err) reject(err.sqlMessage);
resolve(res);
connection.end();
});
}
})
};
Modified which gave me the error PROTOCOL_INCORRECT_PACKET_SEQUENCE:
const _dbQuery = async (queryString, values = null) => {
return new Promise((resolve, reject) => {
try {
const connection = mysql.createConnection({
...configuration.mysql,
});
connection.connect((err) => {
if (err) reject(new Error(err.sqlMessage));
});
if (values !== null) {
connection.query(queryString, values, (err, res, fields) => {
if (err) reject(new Error(err.sqlMessage));
resolve(res);
connection.end();
});
} else {
connection.query(queryString, (err, res, fields) => {
if (err) reject(new Error(err.sqlMessage));
resolve(res);
connection.end();
});
}
} catch (err) {
throw err;
}
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
