Getting undefined value while connecting SQL Server to nodejs with mssql package


I am getting the error below while connecting to SQL Server from node.js using mssql package.

PS F:\Visual Studio Code\NodeSQLServer> node dboperations.js
TypeError: Cannot read properties of undefined (reading 'port')
    at new ConnectionPool (F:\Visual Studio Code\NodeSQLServer\node_modules\mssql\lib\base\connection-pool.js:60:36)
    at new ConnectionPool (F:\Visual Studio Code\NodeSQLServer\node_modules\mssql\lib\tedious\connection-pool.js:10:1)
    at Object.connect (F:\Visual Studio Code\NodeSQLServer\node_modules\mssql\lib\global-connection.js:18:24)
    at getResult (F:\Visual Studio Code\NodeSQLServer\dboperations.js:6:36)
    at Object.<anonymous> (F:\Visual Studio Code\NodeSQLServer\dboperations.js:16:1)
    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)

I have created two js files to fetch required data from SQL Server.

First file is dbconfig.js. Code of the file is below:

const config = {
    user : 'Tableau',
    password : 'tableau@2023',
    server : 'DESKTOP-I933IRB',
    database : 'Rane',
    port : 1433,
    options : {
        trustedConnection : true,
        enableArithAbort : true,
        instancename : 'SQLEXPRESS',
        encrypt : true,
        trustServerCertificate : true

module.exports = config;

second file is dboperations.js with the code below:

let { config } = require('./dbconfig');
const sqlServer = require('mssql');

const getResult = async ()=>{
        let pool = await sqlServer.connect(config);
        let result = await pool.request().query('Select top 5 * From config_table_master');
        return result.recordsets;        

getResult().then(result => {

Besides, the query is running fine on SSMS.

Result of the query used in node

Please, let me know why the error occurred and how can I avoid such errors in the future.


You should change your import statement since you are doing a default export:

let config = require('./dbconfig');

For more info check this out.

Answered By – lpizzinidev

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published