Mysql2 not found in Netlify functions

site name: treasury-cicd.netlify.app

Hi!

When deploying netlify functions that use sequelize I run into problems with mysql2. Build and deploy always work without errors or warnings.

If I’m not importing it anywhere I get the following output at runtime.

Aug 17, 05:30:17 PM: 2a53deed 2023-08-17T15:30:15.907Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Error","errorMessage":"Please install mysql2 package manually","stack":["Error: Please install mysql2 package manually","    at ConnectionManager._loadDialectModule (/var/task/functions/invoiceAssetUploadUrl.js:35750:19)","    at new ConnectionManager (/var/task/functions/invoiceAssetUploadUrl.js:41246:25)","    at new MysqlDialect (/var/task/functions/invoiceAssetUploadUrl.js:41598:34)","    at new Sequelize (/var/task/functions/invoiceAssetUploadUrl.js:47173:24)","    at Function.create (/var/task/functions/invoiceAssetUploadUrl.js:47654:16)","    at Function.createClient (/var/task/functions/invoiceAssetUploadUrl.js:47645:49)","    at Object.<anonymous> (/var/task/functions/invoiceAssetUploadUrl.js:106508:71)","    at Module._compile (node:internal/modules/cjs/loader:1256:14)","    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)","    at Module.load (node:internal/modules/cjs/loader:1119:32)"]}Aug 17, 05:30:17 PM: 2a53deed 2023-08-17T15:30:18.057Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Error","errorMessage":"Please install mysql2 package manually","stack":["Error: Please install mysql2 package manually","    at ConnectionManager._loadDialectModule (/var/task/functions/invoiceAssetUploadUrl.js:35750:19)","    at new ConnectionManager (/var/task/functions/invoiceAssetUploadUrl.js:41246:25)","    at new MysqlDialect (/var/task/functions/invoiceAssetUploadUrl.js:41598:34)","    at new Sequelize (/var/task/functions/invoiceAssetUploadUrl.js:47173:24)","    at Function.create (/var/task/functions/invoiceAssetUploadUrl.js:47654:16)","    at Function.createClient (/var/task/functions/invoiceAssetUploadUrl.js:47645:49)","    at Object.<anonymous> (/var/task/functions/invoiceAssetUploadUrl.js:106508:71)","    at Module._compile (node:internal/modules/cjs/loader:1256:14)","    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1310:10)","    at Module.load (node:internal/modules/cjs/loader:1119:32)"]}Aug 17, 05:30:17 PM: 2a53deed Unknown application error occurredAug 17, 05:30:17 PM: 2a53deed Runtime.UnknownAug 17, 05:30:18 PM: 2a53deed Duration: 892.02 ms	Memory Usage: 73 MB

However if I do require the module (i.e. `import ‘mysql2’) the error changes to

Aug 17, 05:39:17 PM: 1e58331f 2023-08-17T15:39:18.427Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'mysql2'\nRequire stack:\n- /var/task/functions/invoiceAssetUploadUrl.js\n- /var/task/invoiceAssetUploadUrl.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'mysql2'","Require stack:","- /var/task/functions/invoiceAssetUploadUrl.js","- /var/task/invoiceAssetUploadUrl.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:997:17)","    at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)","    at start (file:///var/runtime/index.mjs:1192:23)","    at file:///var/runtime/index.mjs:1198:1"]}Aug 17, 05:39:17 PM: 1e58331f 2023-08-17T15:39:19.048Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'mysql2'\nRequire stack:\n- /var/task/functions/invoiceAssetUploadUrl.js\n- /var/task/invoiceAssetUploadUrl.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'mysql2'","Require stack:","- /var/task/functions/invoiceAssetUploadUrl.js","- /var/task/invoiceAssetUploadUrl.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:997:17)","    at Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)","    at start (file:///var/runtime/index.mjs:1192:23)","    at file:///var/runtime/index.mjs:1198:1"]}Aug 17, 05:39:17 PM: 1e58331f Unknown application error occurredAug 17, 05:39:17 PM: 1e58331f Runtime.ImportModuleErrorAug 17, 05:39:18 PM: 1e58331f Duration: 587.18 ms	Memory Usage: 46 MB

I`ve tried adding both ‘mysql2’ and ‘sequelize’ to external_node_modules and adding a build command to explicitly install them, however it doesn’t work.

Any help would be appreciated.

Please share a reproduction repo to check this.

Sorry, I forgot to came back earlier. I was able to solve it importing the module directly in Sequelize

import { Sequelize } from 'sequelize';
import mysql2 from 'mysql2';

new Sequelize(config.uri, {
    dialect: 'mysql',
    dialectModule: mysql2
});

thanks for sharing your solution with the community.