Node.js ‘fs’ throws an ENOENT error after adding auto-generated Swagger server code

Preamble

To start off, I’m not a developer; I’m just an analyst / product owner with time on their hands. While my team’s actual developers have been busy finishing off projects before year-end I’ve been attempting to put together a very basic API server in Node.js for something we will look at next year.

I used Swagger to build an API spec and then used the Swagger code generator to get a basic Node.js server. The full code is near the bottom of this question.

The Problem

I’m coming across an issue when writing out to a log file using the fs module. I know that the ENOENT error is usually down to just specifying a path incorrectly, but the behaviour doesn’t occur when I comment out the Swagger portion of the automatically generated code. (I took the logging code directly out of another tool I built in Node.js, so I’m fairly confident in that portion at least…)

When executing npm start, a few debugging items write to the console:

"Node Server Starting......
Current Directory:/mnt/c/Users/USER/Repositories/PROJECT/api
Trying to log data now! 
 Mock mode: disabled 
PostgreSQL Pool created successfully
Your server is listening on port 3100 (http://localhost:3100)
Swagger-ui is available on http://localhost:3100/docs"

but then fs throws an ENOENT error:

events.js:174
      throw er; // Unhandled 'error' event
      ^

 Error: ENOENT: no such file or directory, open '../logs/logEvents2021-12-24.log'
Emitted 'error' event at:
    at lazyFs.open (internal/fs/streams.js:277:12)
    at FSReqWrap.args [as oncomplete] (fs.js:140:20)

Investigating

Now normally, from what I understand, this would just mean I’ve got the paths wrong. However, the file has actually been created and the first line of the log file has been written just fine.

My next thought was that I must’ve set the fs flags incorrectly, but it was set to ‘a’ for append:

var logsFile = fs.createWriteStream(__logdir+"/logEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
                    console.error('Could not write new Log File to location: %s nWith error description: %s',__logdir, err);
                });

Removing Swagger Code

Now here’s the weird bit: if I remove the Swagger code, the log files write out just fine and I don’t get the fs exception!
This is the specific Swagger code:

// swaggerRouter configuration
var options = {
    routing: {
        controllers: path.join(__dirname, './controllers')
    },
};

var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, '/api/openapi.yaml'), options);
var app = expressAppConfig.getApp();


// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
    console.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
    console.info('Swagger-ui is available on http://localhost:%d/docs', serverPort);
}).on('error',console.error);

When I comment out this code, the log file writes out just fine.

The only thing I can think that might be happening is that somehow Swagger is modifying (?) the app’s working directory so that fs no longer finds the same file?

Full Code

'use strict';

var path = require('path');
var fs = require('fs');
var http = require('http');

var oas3Tools = require('oas3-tools');
var serverPort = 3100;

// I am specifically tried using path.join that I found when investigating this issue, and referencing the app path, but to no avail

const __logdir = path.join(__dirname,'./logs');
//These are date and time functions I use to add timestamps to the logs
function dateNow(){
    var dateNow = new Date().toISOString().slice(0,10).toString();
    return dateNow
}
function rightNow(){
    var timeNow = new Date().toTimeString().slice(0,8).toString();
    return "["+timeNow+"]  "
};

console.info("Node Server Starting......");
console.info("Current Directory: " + __dirname)

//  Here I create the WriteStreams

var logsFile = fs.createWriteStream(__logdir+"/logEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
                    console.error('Could not write new Log File to location: %s nWith error description: %s',__logdir, err);
                });
var errorsFile = fs.createWriteStream(__logdir+"/errorEvents"+dateNow()+'.log',{flags: 'a'},(err) =>{
                    console.error('Could not write new Error Log File to location: %s nWith error description: %s',__logdir, err);
                });

// And create an additional console to write data out:

const Console = require('console').Console;
var logOut = new Console(logsFile,errorsFile);
console.info("Trying to log data now!") // Debugging logging
logOut.log("========== Server Startup Initiated ==========");
logOut.log(rightNow() + "Server Directory: "+ __dirname);
logOut.log(rightNow() + "Logs directory: "+__logdir);

// Here is the Swagger portion that seems to create the behaviour. 
//   It is unedited from the Swagger Code-Gen tool

// swaggerRouter configuration
var options = {
    routing: {
        controllers: path.join(__dirname, './controllers')
    },
};

var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, '/api/openapi.yaml'), options);
var app = expressAppConfig.getApp();


// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
    console.info('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
    console.info('Swagger-ui is available on http://localhost:%d/docs', serverPort);
}).on('error',console.error);

In case it helps, this is the project’s file structure . I am running this project within a WSL instance in VSCode on Windows, same as I have with other projects using fs.

Is anyone able to help me understand why fs can write the first log line but then break once the Swagger code gets going? Have I done something incredibly stupid?
Appreciate the help, thanks!