When I run the project from Visual Studio Code using npm start in the command line, it runs smoothly without any errors. However, when attempting to run the project as a Windows service, I encounter an error related to the PORT setting. The PORT value of 8080 is initialized in the environment file (env. file).
Below is the content of the env. file:
NODE_ENV=production
# hapi server config
PORT=8080
HOST=localhost
HOST_URL=http://localhost:8080
COOKIE_ENCRYPT_PWD=xxxxxxxxxx...
#sql server config
SQL_USER=xxxxx
SQL_PASSWORD=xxxxxxxx
SQL_DATABASE=xxxxxxxx
SQL_SERVER=computer\SQLEXPRESS
SQL_ENCRYPT=false
#okta config
OKTA_ORG_URL=https://dev-000000.okta.com
OKTA_CLIENT_ID=xxxxxxxx
OKTA_CLIENT_SECRET=xxxxxxxxxxx
Here is the configuration file:
"use strict";
const dotenv = require( "dotenv" );
const assert = require( "assert" );
dotenv.config();
const {
PORT,
HOST,
HOST_URL,
COOKIE_ENCRYPT_PWD,
SQL_SERVER,
SQL_PORT,
SQL_DATABASE,
SQL_USER,
SQL_PASSWORD,
OKTA_ORG_URL,
OKTA_CLIENT_ID,
OKTA_CLIENT_SECRET
} = process.env;
const sqlEncrypt = process.env.SQL_ENCRYPT === "false";
assert( PORT, "PORT is required" );
assert( HOST, "HOST is required" );
module.exports = {
port: PORT,
host: HOST,
url: HOST_URL,
cookiePwd: COOKIE_ENCRYPT_PWD,
sql: {
server: SQL_SERVER,
port: SQL_PORT,
database: SQL_DATABASE,
user: SQL_USER,
password: SQL_PASSWORD,
options: {
encrypt: sqlEncrypt,
enableArithAbort: true
}
},
okta: {
url: OKTA_ORG_URL,
clientId: OKTA_CLIENT_ID,
clientSecret: OKTA_CLIENT_SECRET
}
};
The error message reads as follows:
assert.js:386
throw err;
^
AssertionError [ERR_ASSERTION]: PORT is required
at Object.<anonymous> (C:\inetpub\apis\routis\src\config.js:25:1)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (C:\inetpub\apis\routis\src\index.js:3:16)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '=='
}