Internal Server Error 500 - Having issues running lowdb via Next.js and Netlify
MadMaxMcKinney opened this issue · comments
I've got a really simple DB setup that works when running locally but doesn't work once it's hosted on Netlify. Is there something special that needs to be done for this to work? I don't know if this is an issue on Next.js or a limitation of lowdb.
Here is my db file (root/db/db.js)
import {Low, JSONFileSync} from 'lowdb'
// Cluster DB Setup
const adapter = new JSONFileSync('cluster-db.json')
const clusterDB = new Low(adapter)
// Initialize if empty
clusterDB.read()
clusterDB.data ||= { clusters: [] }
clusterDB.write()
export {clusterDB}
And my only API route (root/pages/api/cluster.js
import {clusterDB} from '../../db/db'
export default async function handler(req, res) {
await clusterDB.read()
switch(req.method) {
case 'POST':
let newCluster = {severity: req.query.severity, comments: req.query.comments, date: req.query.date}
clusterDB.data.clusters.push(newCluster)
clusterDB.write()
res.status(200).json({status: "Success", cluster: newCluster})
break;
case 'GET':
if(clusterDB.data.clusters) {
res.status(200).json(clusterDB.data.clusters)
} else {
res.status(404).json({status: "404"})
}
break;
}
res.status(200).json({test: "yay"})
}
I don't get any other error info besides a 500 error on the server-side. I get the error even if all I do is import the clusterDB
object, so something is happening there.
I just face this error and found a work around. JSONFileSync read file in runtime, netlify funtions won't include them in build. So I import them into my script import jpJson from "../data/jp.json"
then write a custom adapter to return jpJson
@thainq3127 Could you elaborate some more? I'm not entirely sure what the root of the problem is. Netlify won't include the Lowdb adapters? Is there a way to get Netlify to better include elements in the build? Running "npm run build && npm run start" also seems to work locally so you are right that something is happening on the Netlify side of things.
Netlify won't include json files, I was too dumb to not google this first https://www.netlify.com/blog/2021/08/12/how-to-include-files-in-netlify-serverless-functions/
Since this issue is not related to lowdb's code, can it be closed?