SyntaxError: Identifier has already been declared



  • Full Error: SyntaxError: Identifier 'archivelog' has already been declared

    Action Performed:

    const fs = require('fs');
    const archivelog = fs.writeFileSync('archive.log', 'This is test text log entry')
    const archivelog = fs.appendFileSync('archive.log', 'This is testing an append to my log file')
    

    Fix/Solution:
    The issues is with the variable scope. As both variables are the same name and in the Global scope. See this link for more information on scopes: https://www.w3schools.com/js/js_scope.asp

    Changing one of the variable names resolved the issues.

    const fs = require('fs');
    const archivelog = fs.writeFileSync('archive.log', 'This is test text log entry')
    const appendArchiveLog = fs.appendFileSync('archive.log', 'This is testing an append to my log file')
    

    Note: This is just to demonstrate the Global variable scope, changing the variable name every time you want to change the archive.log file is not practical or the right way to code.



© Lightnetics 2024