RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received type number (NaN)

Hi,

I am stuck for almost one year in my fullstack project. I am now starting the fullstack project new and facing other issue as the title describe.

PSQL in my local machine has the same value for pooling as my .env in github. However, the above issue persists.

Would anyone assist me to solve the problem?

Check my github repo: simple-e-commerce/server/config/index.js at main · BiruSkai/simple-e-commerce · GitHub.

Below is the error info after running nodemon app.js:

node:internal/validators:424 throw new ERR_SOCKET_BAD_PORT(name, port, allowZero); ^

RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received type number (NaN). at validatePort (node:internal/validators:424:11) at Server.listen (node:net:2000:5) at Function.listen (D:\project coding 1.1.2024\FullStack\simple-e-commerce\server\node_modules\express\lib\application.js:635:24) at Object. (D:\project coding 1.1.2024\FullStack\simple-e-commerce\server\app.js:30:5) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions…js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) at node:internal/main/run_main_module:28:49 { code: ‘ERR_SOCKET_BAD_PORT’

I have extra info that could lead to solve this problem. Imo, the problem lies on the app translates the express port in config/index.js to its values in app,js.


File- config/index.js:

require("dotenv").config();
const {Pool} = require("pg");
const isProduction = process.env.NODE_ENV === "production";
const connectionString = `postgresql://${process.env.USER}:${process.env.PASS}@${process.env.HOST}:${parseInt(process.env.PORT)}/${process.env.DBNAME}`;

const pool = new Pool({
        connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
        ssl: {
                rejectUnauthorized: false
        }
});

module.exports = {
        port: parseInt((process.env.EXPRESS_PORT)),
        pool
};

File app,js

const express = require("express")
const config = require("./config")
const routes = require("./routes")
const app = express()

app.use((error, req, res, next) => {
        res.status(error.status || 500).send({
                error: {
                        status: error.status || 500,
                        message: error.message || "Internal server error"
                }
        })
})

const PORT = config.port

console.log(PORT)
console.log(typeof(PORT))

app.listen(PORT, () => {
        console.log(`Server is listening to port: ${PORT}.`)
})

If I run debug, the result in debug console is:
3000
number
Server is listening to port: 3000.


If I run “nodemon app,js” in server folder, the result is the error in this title.


Has anybody clue, so that if run nodemon app.js, the result will be “Server is listening to port: 3000.” ?

Hi @biruskai are you still looking for a solution on this? If not, have you also tried posting in the CodeCademy Discord? I usually find people to be very helpful there.

I can try and look into it sometime in the next few days but don’t want to promise anything as I’m pretty time poor atm.

Hi, I have not yet found a solution for this issue.
I would appreciate your time if you could help me.
At the same time , I will post it in Discord and let you know if the correct answer comes faster than yours.

Would this help?

Hi, Thanks for reviewing my query. It has the same title, but it does not solve my problem. I read this solution before I posted the query in this forum and someone deleted my query in that exact stackoverflow page.
But, fortunately I have found the solution. The issue lied on File- config/index.js. I need to define the path dotenv.config to .env File. Below is the solution:

in File config/index.js:

Before:
require(“dotenv”).config();
After:
require(“dotenv”).config({path:“…/.env”});