An unexpected AssertionError was encountered: The PORT parameter must be specified when launching node.js as a Windows service

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: '=='
}

Answer №1

Make sure to check the file extension for dotenv files, which should be .env.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am having trouble loading materials from JSONLoader in Three.js

Hi there, I am working on implementing a simple TheeJS code that will allow me to load some 3D js files using JSONLoader. These 3D files were exported from Blender exporter, but when I run this part of the code, I encounter two errors: Uncaught TypeError: ...

Eliminate the registration message from TinyMCE by importing a package

Looking to create a TinyMCE React package, I've been using import { Editor } from '@tinymce/tinymce-react'; However, I'm encountering this message - https://i.sstatic.net/j4Cfw.png To remove the message, typically you would add the AP ...

Creating a JSON object in AngularJS is a simple and straightforward process

Is it a good practice to create a JSON object in AngularJS this way? Or is there a better solution to achieve the desired format? Edit question: I am trying to create an object in JSON format as shown below. I have written the code but facing difficulty ...

The persistent Bulma dropdown glitch that refuses to close

In the project I'm working on, I have implemented a Bulma dropdown. While the dropdown functions correctly, I am facing an issue when adding multiple dropdowns in different columns with backend integration. When one dropdown is open and another is cli ...

Guide on looping through a collection of objects that have child objects and generating a new custom object

I am currently working with an object list retrieved from an API. The response looks something like this: { "1/22/20": { "new_daily_deaths": 0, "total_cases": 1, }, "1/23/20": { "new_deaths": 0 ...

How to bypass CORS restrictions in XMLHttpRequest by manipulating HTTP headers?

Currently experimenting with the (deprecated) Twitter API 1.0 For instance, I am interested in retrieving data from the API utilizing AJAX browser requests on cross-origin web pages. This could be a new tab, a local HTML file, or any established website. ...

Struggling to make EJS button functional on the template

I am currently facing an issue with a loop that populates a webpage with multiple items, each containing an image, text, button, and a unique ID associated with it. Although I have written a function to retrieve the ID and plan name when the button is clic ...

Step-by-step guide to performing an AJAX request in JavaScript while using Ubuntu

My current setup involves using a JavaScript file in conjunction with NodeJS to execute AJAX calls. To accomplish this, I have installed and imported jQuery as demonstrated below: var http = require("http"); $ = require("jquery"); test(); funct ...

Error: Unable to access the 'setState' property of an undefined value - Pokemon API

Currently, I am attempting to pass text input data from one component to another by using params. However, an error has occurred: A TypeError has been thrown: 'Cannot read property 'setState' of undefined' I am contemplating whether ...

The java servlet for multi-input form validation using ajax is malfunctioning as it fails to display any error messages

My Ajax form validation is not functioning properly. Despite the code provided below, errors are not being displayed when the text fields are left empty. <form id="postMveForm" action="movieDetails"> <input type="text" name="mvetitle" id="mveTit ...

Working with AJAX PHP variables within JavaScript

The code below was previously functioning correctly: var xmlHttp var layername var url function update(layer, url) { var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere if(xmlHttp==null) { alert("Your browser is not supporte ...

Retrieving a property of an object within a function

I'm facing an issue where I am trying to access the properties of objects inside an array in my code to display text values in input boxes that are recovered from local storage after a refresh. However, when I attempt to run a for loop within my appSt ...

After three consecutive occurrences of readyState being set to 4, an Ajax error was triggered by an alert

I encountered a peculiar situation. If you try double clicking on "Insane" on this fiddle, you will observe that the onreadystatechange function is triggered 3 times, all with readyState = 4. To me, this seems quite bizarre. What's even more surpris ...

Just getting started with JavaScript and running into trouble creating an array of image objects using an array of strings

I am struggling to accurately describe my issue, making it difficult to find a solution. I have an array called tempData generated from a text file that I want to reference with variables of the same name as the strings in the array. var red = new Image ...

Is it possible to create a dynamic HTML element with an AngularJS Directive without using $scope?

Looking to use This Plunker example code to dynamically add elements to an HTML page using AngularJS. (Make sure to run it in a separate link, not in the editor environment.) This will be my first time working with AngularJS Directives (aside from simple ...

Discovering if an array includes a particular value in JavaScript/jQuery

Is there a way to check if the list with the class .sidebar contains an item with data-id="1"? <ul class="sidebar"> <li data-id="1">Option 1</li> <li data-id="2"> Option 2</li> <li data-id="3"> Option 3</li&g ...

Transforming the *.vue file into a *.js file for deployment on a content delivery network

Is there a way to compile Component.vue files into JavaScript files for use with the Vue CDN in HTML? For example, consider the following component Component.vue: <template> <div class="demo">{{ msg }}</div> </template& ...

Looking to position content at the vertical midpoint for a polished look?

Struggling to center the 'MAIN' content vertically in the middle of the page despite trying various methods. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

As a prop undergoes changes, the constant reverts back to its original values

I created a filter component for a table... The handleSearch function updates the const filters perfectly when the dataIndex props remain the same. However, when it changes, the filters value resets back to an empty array. Despite console logging everyth ...

Adjusting Headers Using Buttons

Having some issues with my JavaScript code. I'm trying to achieve a specific functionality where clicking the "Modify HTML content" button changes the h1 heading from "The Original Content" to "The New Content", and vice versa on subsequent clicks. Si ...