Error invalid ELF header SQLite3

Hey, I’m having trouble with serverless functions using the SQLite3 database. In this case, when I tried it on my own PC, it worked perfectly fine. But in Netlify, it gave me an error: “Error: /var/task/node_modules/sqlite3/build/Release/node_sqlite3.node: invalid ELF header.”. I was wondering if anyone will help me out.

Stack Trace

Error: /var/task/node_modules/sqlite3/build/Release/node_sqlite3.node: invalid ELF header
    at Module._extensions..node (node:internal/modules/cjs/loader:1452:18)
    at Module.load (node:internal/modules/cjs/loader:1197:32)
    at Module._load (node:internal/modules/cjs/loader:1013:12)
    at Module.require (node:internal/modules/cjs/loader:1225:19)
    at require (node:internal/modules/helpers:177:18)
    at bindings (/var/task/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/var/task/node_modules/sqlite3/lib/sqlite3-binding.js:1:37)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)
    at Module.load (node:internal/modules/cjs/loader:1197:32)

netlify.toml

[build]
    functions = "functions"
[functions]
  external_node_modules = ["express", "serverless-http", "sqlite3"]
  node_bundler = "esbuild"
[[redirects]]
  force = true
  from = "/api/*"
  status = 200
  to = "/.netlify/functions/api/:splat"

packages.json

{
  "name": "p",
  "module": "functions/api.ts",
  "type": "module",
  "devDependencies": {
    "@types/express": "^4.17.21",
    "@types/bun": "latest"
  },
  "dependencies": {
    "@netlify/functions": "^2.6.0",
    "express": "^4.19.2",
    "netlify-cli": "^17.22.1",
    "serverless-http": "^3.2.0",
    "sqlite3": "^5.1.7"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  }
}

Do you think there’s any problem that cause it? Let me know!

Hi, @Stawa. I have a suggestion and a concern. The suggestion is to make certain you are bundling the database file in the function using the included_files option:

My concern is about using SQLite in a serverless function. Are you using the database read-only or are you trying to write to it?

I ask because the filesystem of the serverless function is not persistent. If you write data to that file in the serverless function those changes won’t necessarily persist in future invocations. Writes to the database will be lost because the database file is in the filesystem and changes to the filesystem in the serverless function are not persistent. (They can persist temporarily but all writes will eventually disappear.)

Again, if you are using SQLite read-only (meaning the database file won’t change) that will work perfectly. However, if you are trying to write to a SQLite database in the serverless function filesystem, that will not work as planned.

1 Like