1

For uni I'm working on node.js servers. I'm following the code used in the lectures and I got in trouble with persistent sessions in express.js. In the lecture slides the following require is used:

var session = require('express-session');
var SQLiteStore = require('sqlite3')(session);

But when I used this in my code I get the following Error:

var SQLiteStore = require("sqlite3")(session);
                                    ^

TypeError: require(...) is not a function
    at Object.<anonymous> (C:\Users\jordv\OneDrive\Documenten\UU\2022 -2023\Blok 3\Web Technology\test\server.js:12:37)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

I tried to find a solution for this online, and found this post NodeJs : TypeError: require(...) is not a function. I tried to understand the top answer but I just can't. I have two questions:

  1. In the linked post, immediately invoked functions are mentioned. Why is my code an immediately invoked function? (My thought is that the error message states that it is not a function, so how is it immediately invoked?)
  2. How is the answer to that post supposed to be applied to my situation?

EDIT: In the comments it was suggested to console.log(sqlite3). this gives

{
  Database: [Function: Database],
  Statement: [Function: Statement],
  Backup: [Function: Backup],
  OPEN_READONLY: 1,
  OPEN_READWRITE: 2,
  OPEN_CREATE: 4,
  OPEN_FULLMUTEX: 65536,
  OPEN_URI: 64,
  OPEN_SHAREDCACHE: 131072,
  OPEN_PRIVATECACHE: 262144,
  VERSION: '3.41.1',
  SOURCE_ID: '2023-03-10 12:13:52 20399f3eda5ec249d147ba9e48da6e87f969d7966a9a896764ca437ff7e737ff',
  VERSION_NUMBER: 3041001,
  OK: 0,
  ERROR: 1,
  INTERNAL: 2,
  PERM: 3,
  ABORT: 4,
  BUSY: 5,
  LOCKED: 6,
  NOMEM: 7,
  READONLY: 8,
  INTERRUPT: 9,
  IOERR: 10,
  CORRUPT: 11,
  NOTFOUND: 12,
  FULL: 13,
  CANTOPEN: 14,
  PROTOCOL: 15,
  EMPTY: 16,
  SCHEMA: 17,
  TOOBIG: 18,
  CONSTRAINT: 19,
  MISMATCH: 20,
  MISUSE: 21,
  NOLFS: 22,
  AUTH: 23,
  FORMAT: 24,
  RANGE: 25,
  NOTADB: 26,
  LIMIT_LENGTH: 0,
  LIMIT_SQL_LENGTH: 1,
  LIMIT_COLUMN: 2,
  LIMIT_EXPR_DEPTH: 3,
  LIMIT_COMPOUND_SELECT: 4,
  LIMIT_VDBE_OP: 5,
  LIMIT_FUNCTION_ARG: 6,
  LIMIT_ATTACHED: 7,
  LIMIT_LIKE_PATTERN_LENGTH: 8,
  LIMIT_VARIABLE_NUMBER: 9,
  LIMIT_TRIGGER_DEPTH: 10,
  LIMIT_WORKER_THREADS: 11,
  cached: { Database: [Function: Database], objects: {} },
  verbose: [Function (anonymous)]
}
Jord van Eldik
  • 351
  • 2
  • 10
  • 1
    Clearly `require('sqlite3')(session);` and `sqlite3(session);` are different. Why do you expect `sqlite3` to be defined? – Pointy Apr 10 '23 at 19:48
  • 1
    You have to declare the variable first `const sqlite3 = require('sqlite3')` – Konrad Apr 10 '23 at 19:49
  • apologies, was trying some things in the hope that that would solve the problem. I've edited the post – Jord van Eldik Apr 10 '23 at 19:51
  • can you post more context? for example the lesson part regarding the session. i am assuming the package being used is this one, is that correct? https://github.com/theogravity/express-session-sqlite – Sombriks Apr 10 '23 at 19:56
  • Try doing `const sqlite3 = require("sqlite3"); console.log(sqlite3);` to see what it's returning. – Barmar Apr 10 '23 at 19:56
  • https://www.npmjs.com/package/sqlite3 makes no mention of the package exporting a function. – Bergi Apr 10 '23 at 19:57
  • @Sombriks we use ```express-session``` and ```sqlite3``` – Jord van Eldik Apr 10 '23 at 19:58
  • 2
    @JordvanEldik are you absolutely sure the exercise says `sqlite3` and shows `var SQLiteStore = require('sqlite3')(session);`? And not about `connect-sqlite3`? Like shown here [Default path for SQLite Store - Node.js](https://stackoverflow.com/questions/49383334) – t.niese Apr 10 '23 at 20:06
  • @t.niese yes I'm sure. But i can divert from the course material if i want. Would replacing `sqlite3` with `connect-sqlite3` not mess with other things? It does indeed resolve the error. – Jord van Eldik Apr 10 '23 at 20:11
  • 1
    @JordvanEldik If it really talks about `sqlite3` and shows `var SQLiteStore = require('sqlite3')(session);` then you should talk with the one who supervises the course, and ask that person which module should be used. We don't know which module should be used and if using a different one would result in later problems. – t.niese Apr 10 '23 at 20:13
  • @t.niese I'll message the supervisor. Thanks for the help! – Jord van Eldik Apr 10 '23 at 20:15
  • If only basic session functionality is used, then I guess it will work because it is just an adapter for the session storage. But it is better to ask about that. – t.niese Apr 10 '23 at 20:19

0 Answers0