File Exists error after upgrade to node-red 3.1.1 (from 2.x)

I have just updated a running system on Windows 10 to the latest stable versions of Node-Red, as follows:

7 Dec 17:18:30 - [info] Node-RED version: v3.1.1
7 Dec 17:18:30 - [info] Node.js  version: v20.10.0
7 Dec 17:18:30 - [info] Windows_NT 10.0.19045 x64 LE
7 Dec 17:18:32 - [info] Loading palette nodes
7 Dec 17:18:35 - [info] Dashboard version 3.6.1 started at /ui
7 Dec 17:18:36 - [info] Settings file  : 
C:\Users\soggy\.node-red\settings.js
7 Dec 17:18:36 - [info] Context store  : 'default' [module=memory]
7 Dec 17:18:36 - [info] User directory : \Users\soggy\.node-red
7 Dec 17:18:36 - [warn] Projects disabled : 
editorTheme.projects.enabled=false
7 Dec 17:18:36 - [info] Flows file     : 
\Users\soggy\.node-red\flows.json
7 Dec 17:18:36 - [info] Server now running at http://127.0.0.1:1880/

Here is the error I get. This is in a function Create New Filename which runs on a timer just after midnight, or at startup, to create a log file with a date stamp. This function just creates the filename, another node actually creates the file.
If I started and stopped the system in the middle of the day, I would loose the first file created. So this logic tests to see if the standard filename already exists, and if so, appends a -1, -2, etc to the filename to create a unique one. I am using fs.existsSync to test if the file exists, and this is now generating an error. Actual code is included below.

My previous settings.js file has an entry in the functionGlobalContext section that says this:
'fs' : require('fs')
But the new settings.js file does not. Is this the source of my error?
Is there a better way to do what I am trying to do?
I created this node red system a few years ago, and I'm having trouble remembering how and why I did certain things. And the latest version may have improved capabilities such that some brute force things I did are no longer necessary.

`TypeError: Cannot read properties of undefined (reading 'existsSync')
     at Function node:dc7f7fe2.72f29 [Create New Filename]:20:10
     at Function node:dc7f7fe2.72f29 [Create New Filename]:69:3
     at Script.runInContext (node:vm:133:12)
     at processMessage
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\nodes\core\function\10-function.js:419:33)
     at FunctionNode._inputCallback
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\nodes\core\function\10-function.js:342:17)
     at
C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:214:26
     at Object.trigger
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\util\lib\hooks.js:166:13)
     at FunctionNode.Node._emitInput
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:206:11)
     at FunctionNode.Node.emit
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:190:25)
     at FunctionNode.Node.receive
(C:\Users\soggy\AppData\Roaming\npm\node_modules\node-red\node_modules\@node-red\runtime\lib\nodes\Node.js:499:10)
TypeError: Cannot read properties of undefined (reading 'existsSync')
`
// Current date/time
var fs = global.get('fs');
var oldExists  = false;
var newExists  = false;
const myPath = "C:\\Users\\soggy\\Desktop\\Node-Red\\";
var d = new Date();
if (msg.topic=="0") {  // If the midnight trigger, set to yesterday's date }
// Get yesterday's date
d.setDate(d.getDate() - 1);
}
// Format into a filename
var dstr = (d.getFullYear() + '-' + ('00' + (d.getMonth()+1)).slice(-2) + '-' + ('00' + d.getDate()).slice(-2));
// If new file already exists, then change the new filename
var newFilename = dstr + "_PortAlt.csv";
var newPath = myPath + "Logs\\" + newFilename;
var oldPath = myPath + "PortAlt.log"; 

try {
  if (fs.existsSync(oldPath)) {
      oldExists=true;
      console.log(oldPath+ " exists!");
  }
  else {
      oldExists=false; 
  }
} catch(err) {
  console.error(err)
  oldExists=false;
}
try {
  if (fs.existsSync(newPath)) {
    newExists=true
    console.log(newPath+ " exists!");
  }
  else { 
      newExists=false; 
  }
} catch(err) {
  console.error(err);
  newExists=false;
}

if (oldExists && newExists) {
    console.log("Both Files Exist!");
    var i=1;
    do {
        newFilename = dstr + "_PortAlt_"+i.toString()+".csv";
//        console.log("Inside Do While, filename: "+newFilename);
        newPath = myPath + "Logs\\" + newFilename;
        i++;
        try {
          if (fs.existsSync(newPath)) {
            newExists=true;
            console.log(newPath+ " exists!");
          }
          else {
            newExists=false;
          }
        } catch(err) {
          console.error(err);
          newExists=false;
        }
    } while (newExists);
}
// Send it off
msg.payload = newFilename;
return msg;

For starters, you can now import fs directly in the function node (setup tab)

But, there is nothing you are doing that couldn't be achieved in a more flow like way.

Install node-red-contrib-fs-ops and you can do it all without a function in sight.

The resolution of this problem, for me, was that I forgot to add in
'fs' : require('fs')
to the settings.js file. Once I did this, the errors went away.

What I'm trying to do is create a file with a date in the file name. Everything logs to a file WITHOUT a specific date in the name, but just after midnight I rename the previous days file to a name with the date in it, and create a new file with a new header.

And you could skip all of that and use something like flogger with built in log rotation

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.