skip to Main Content

I’m a bit of a novice at JavaScript coding in the VS Code script editor.

I’m trying to read in a .txt file as an array.

Below is my code:

const fs = require('fs');

function readFile(filePath) {

    try {
        const data = fs.readFileSync(filePath);
        data.toString();        
    }
    catch (error) {
        console.error(`Got an error trying to read the file: ${error.message}`);
    }

}

const input = readFile('G:/My Drive/Visual Studio Code/Advent of Code/2022/day05.txt');

console.log(input)

The text file is taken from the sample data found here. This is how it is in my text file in VS Code.

    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

When I run the script, I get the below message in the OUTPUT window. I don’t understand why getting an undefined message for my .js file. It was working fine earlier. I made no changes to the script, but I’m getting this message now.

enter image description here

FYI – The .js and .txt file are saved in my Google Drive, but I don’t see how that could be the problem. The undefine message only appears when I add the console.log() code.

Any help would be appreciated!

Many thanks in advance!

2

Answers


  1. The input you receive is just a Function Object, you need to return the value that fs can read.
    Follow below:

    function readFile(filePath) {
    
        try {
            const data = fs.readFileSync(filePath);
            return data.toString();        
        }
        catch (error) {
            console.error(`Got an error trying to read the file: ${error.message}`);
        }
    }
    
    Login or Signup to reply.
  2. const fs = require('fs');
    
    function readFile(filePath) {
        try {
            // Read the file synchronously and return its content as a string
            return fs.readFileSync(filePath).toString();
        } catch (error) {
            console.error(`Got an error trying to read the file: ${error.message}`);
            // Return an empty string in case of an error
            return '';
        }
    }
    
    const input = readFile('G:/My Drive/Visual Studio Code/Advent of Code/2022/day05.txt');
    
    console.log(input);
    

    Install Node.js:
    Make sure you have Node.js installed on your machine. You can download it from the official website: Node.js Downloads.

    Create a JavaScript File:
    Copy the code you provided into a file. Save it with a ".js" extension, for example, "Day_05.js".

    Open a Terminal or Command Prompt:
    Open a terminal or command prompt in the directory where you saved your JavaScript file.

    Run the Script:
    Type the following command and press Enter to run your script:

    bash
    Copy code

    node Day_05.js
    

    This assumes that you are in the same directory as your JavaScript file.

    Review Output:
    If everything is set up correctly, you should see the output in the terminal, which will be the content of the file specified in readFile function (in this case, ‘G:/My Drive/Visual Studio Code/Advent of Code/2022/day05.txt‘).

    Note: Make sure that the file path you specified in readFile function is correct and the file exists. If the file doesn’t exist or there’s an issue with the path, the readFile function will catch an error, and you will see an error message in the console.

    That’s it! This should execute your Node.js script and display the content of the specified file in the console.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search