Good day,
I decided to use Knexjs within my netlify functions to avoid SQL Injection and some other security issues. It is working locally without any issues, but at the moment of deploying the application, I get the following error:
{"errorType":"Error","errorMessage":"Knex: run\n$ npm install mysql --save\nCannot find module 'mysql'\nRequire stack:\n- /var/task/node_modules/knex/lib/dialects/mysql/index.js\n- /var/task/node_modules/knex/lib/dialects/index.js\n- /var/task/node_modules/knex/lib/knex-builder/internal/config-resolver.js\n- /var/task/node_modules/knex/lib/knex-builder/Knex.js\n- /var/task/node_modules/knex/lib/index.js\n- /var/task/node_modules/knex/knex.js\n- /var/task/database/knex.js\n- /var/task/netlify/functions/server.js\n- /var/task/server.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js","stack":["Error: Knex: run","$ npm install mysql --save","Cannot find module 'mysql'","Require stack:","- /var/task/node_modules/knex/lib/dialects/mysql/index.js","- /var/task/node_modules/knex/lib/dialects/index.js","- /var/task/node_modules/knex/lib/knex-builder/internal/config-resolver.js","- /var/task/node_modules/knex/lib/knex-builder/Knex.js","- /var/task/node_modules/knex/lib/index.js","- /var/task/node_modules/knex/knex.js","- /var/task/database/knex.js","- /var/task/netlify/functions/server.js","- /var/task/server.js","- /var/runtime/UserFunction.js","- /var/runtime/Runtime.js","- /var/runtime/index.js"," at Client_MySQL.initializeDriver (/var/task/node_modules/knex/lib/client.js:194:13)"," at new Client (/var/task/node_modules/knex/lib/client.js:75:12)"," at new Client_MySQL (/var/task/node_modules/knex/lib/dialects/mysql/index.js:21:1)"," at Function.knex (/var/task/node_modules/knex/lib/knex-builder/Knex.js:16:28)"," at Object.<anonymous> (/var/task/database/knex.js:4:41)"," at Module._compile (internal/modules/cjs/loader.js:1085:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"," at Module.load (internal/modules/cjs/loader.js:950:32)"," at Function.Module._load (internal/modules/cjs/loader.js:790:12)"," at Module.require (internal/modules/cjs/loader.js:974:19)"]}
Here is the part where I use Knexjs:
//knex.js within database folder
const variables = require('../current_ntl_env_variables.json');
const config = variables["knexConfig"];
module.exports = require('knex').default(config);
//server.js function
const knex = require('../../database/knex.js');
...
//implementation
router.get('/countries', async (_,res) => {
knex('country')
.select('*')
.then((countries) => {
return res.json(countries);
}).catch((err)=>{
res.status(400).send(err);
});
});
And here is the package.json where I install all required dependencies:
"dependencies": {
"@angular/animations": "~14.0.2",
"@angular/cdk": "^14.0.2",
"@angular/common": "~14.0.2",
"@angular/compiler": "~14.0.2",
"@angular/core": "~14.0.2",
"@angular/flex-layout": "^13.0.0-beta.38",
"@angular/forms": "~14.0.2",
"@angular/material": "^14.0.4",
"@angular/platform-browser": "~14.0.2",
"@angular/platform-browser-dynamic": "~14.0.2",
"@angular/router": "~14.0.2",
"dotenv": "^16.0.1",
"encoding": "^0.1.13",
"express": "^4.18.1",
"knex": "^2.1.0",
"moment": "^2.29.3",
"mysql": "^2.18.1",
"netlify-cli": "^10.7.1",
"rxjs": "~7.5.5",
"serverless-http": "^3.0.1",
"tslib": "^2.4.0",
Here is a related topic I found in which they state it’s because my functions dependencies have not been installed properly but I have a root package.json holding all dependencies. Any ideas?
I have not been able to make simple netlify functions work Thanks for the help