JLuboff / connect-mssql-v2

MS SQL Server session store for Express Session
MIT License
5 stars 7 forks source link

TypeError: require(...) is not a function #31

Closed EstebanHelpDesk closed 3 years ago

EstebanHelpDesk commented 3 years ago

Hi, first at all, i'm super new in express (also in javascript).

I'm trying to use this functionality, but I try everything and cant do it work. (everything I know :) )

I started from scratch to avoid problem with other components.

I just load dotenv, mssql, express, express-session and connect-mssql-v2

const express = require("express");
require("dotenv").config();
const session = require("express-session");
const MSSQLStore = require("connect-mssql-v2")(session); // in "session" word node says "TypeError: require(...) is not a function"
const sql = require("mssql");

const app = express();

var db_enableArithAbort;

if (process.env.db_enableArithAbort == "true") {
  db_enableArithAbort = true;
} else {
  db_enableArithAbort = false;
}

const config = {
  user: process.env.db_user,
  password: encodeURIComponent(process.env.db_password),
  server: process.env.db_host,
  port: Number(process.env.db_port),
  database: process.env.db_database,
  encrypt: process.env.db_encrypt,
  options: {
    appName: process.env.db_appname,
    enableArithAbort: db_enableArithAbort,
  },
};
/*
const poolPromise = new sql.ConnectionPool(config)
  .connect()
  .then(pool => {
    console.log('Connected to MSSQL -> '+config.server+':'+config.port+'/'+config.database)
    return pool
  })
  .catch(err => console.log('Database Connection Failed! Bad Config: ', err))
*/

app.use(
  session({
    store: new MSSQLStore(config), // options are optional
    secret: "supersecret",
  })
);

/* START SERVER */
app.set("port", process.env.WEB_PORT || 8001);
var server = app.listen(app.get("port"), function () {
  console.log("Sever Running. Port " + server.address().port);
});

If I comment the lines associated to connect-mssql I was able to run a simple query from my poolpromise with a request and query... that part works fine. That is I know SQL config is OK.

Can you help me?

I also have some doubt about using... on a real project, I want to store some value per user/session and be able to recover this value in different situations or when I using some routes... can you add some example "for dummies"?

Sorry my english is bad.

bradtaniguchi commented 3 years ago

I just updated your code by passing it thru prettier and added some syntax highlighting :+1:

Whats your node version (can be found using node -v) and whats your package.json look like? Your the second person to have ran into this issue recently but we haven't been able to replicate it 🤔

EstebanHelpDesk commented 3 years ago

Hi. thank you for your reply.

Here is my package.json

{
  "name": "session",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon main",
    "start": "main"
  },
  "author": "esteban",
  "license": "ISC",
  "dependencies": {
    "connect-mssql-v2": "^1.6.2",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-session": "^1.17.1",
    "mssql": "^6.2.3",
    "nodemon": "^2.0.4"
  }

}

My node version is v12.18.3

JLuboff commented 3 years ago

Using the provided package.json and main.js, I was able to replicate. Thank you for providing that. I'll have to take a look but may not get to it right away unfortunately. @bradtaniguchi if you have the opportunity to look, that'd be great otherwise, I'll make sure to get to it.

JLuboff commented 3 years ago

Ok, I believe I have figured out the issue. We're currently using export default Store so for JS, we would need const MSSQLStore = require('connect-mssql-v2').default(session). This is not ideal. I originally thought we can change to use export const Store = ... but that breaks default imports. I'll be looking into this further. Ignore my last commit to the fixrequire branch as it does not actually fix the issue.

JLuboff commented 3 years ago

@EstebanHelpDesk issue has been resolved in latest version (1.6.3) thanks to @bradtaniguchi

EstebanHelpDesk commented 3 years ago

I would like to thank you both!

I already updated new ".d.ts" file in my node_modules. But I have a doubt, must I return to original format?

I need to keep "patched" version of this const MSSQLStore = require('connect-mssql-v2').default(session)

or return to original ? const MSSQLStore = require('connect-mssql-v2').(session)

I was trying to follow all your comments, but my JS experience is almost zero.

bradtaniguchi commented 3 years ago

The correct format should of always been:

const MSSQLStore = require('connect-mssql-v2')(session);

this should match the format of most other session stores for express-session

EstebanHelpDesk commented 3 years ago

Thank you @bradtaniguchi and @JLuboff . I confirm it is running now.