Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have node.js application I am setting .env using dotenv, I get the following error when using port

Tags:

node.js

dotenv

I have node.js application I have installed dotenv and add the following configuration in my .env file

 DB_HOST='localhost'
 DB_Database=TheDatabasename
 DB_USER=TheUser
 DB_PASS=thePassword
 DB_PORT=1433

I am using sqlserver I call dotenv like bellow:

const sql = require('mssql');
const dotenv = require('dotenv');
dotenv.config();

const config = {
 user: process.env.DB_USER,
 password: process.env.DB_PASS,
 server: process.env.DB_HOST, 
 database: process.env.DB_Database,
 port: process.env.DB_PORT,
}

 const poolPromise = new sql.ConnectionPool(config)
.connect()
.then(pool => {
 console.log('Connected to MSSQL')
 return pool
})
 .catch(err => console.log('Database Connection Failed! Bad Config: ', err))

 module.exports = {
  sql, poolPromise
 }

I get the following error:

  Database Connection Failed! Bad Config:  TypeError: The "config.options.port" property must be of type number.
like image 994
MJ X Avatar asked Nov 04 '19 19:11

MJ X


1 Answers

Mssql, require a type number in the port parameter and the environment variables are string.

You should cast the value before sent to the driver

...
 port: parseInt(process.env.DB_PORT, 10),
...
like image 189
Yeinso Blanco Avatar answered Sep 28 '22 09:09

Yeinso Blanco