最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Unexpected Identifier when reading txt file in node - Stack Overflow

matteradmin6PV0评论

I'm a beginner in node, I want to create a list data structure from reading a txt file, here is my code to read the file.

        var fs = require("fs");
        var filename = require("../films.txt");
        
        fs.readFile(filename, 'utf8', function(err,data){
          var contents = data;
          var splitContents = contents.split("\n");
          var string = JSON.stringify(splitContents);
          console.log(string)
        });
    
    

And below this line is the text file I want to read is 20 movies, the name of the file is films.txt.

    1. The Shawshank Redemption
    2. The Godfather
    3. The Godfather: Part II
    4. Pulp Fiction
    5. The Good, the Bad and the Ugly
    6. 12 Angry Men
    7. Schindler’s List
    8. The Dark Knight
    9. The Lord of the Rings: The Return of the King
    10. Fight Club
    11. Star Wars: Episode V - The Empire Strikes Back
    12. One Flew Over the Cuckoo’s Nest
    13. The Lord of the Rings: The Fellowship of the Ring
    14. Inception
    15. Goodfellas
    16. Star Wars
    17. Seven Samurai
    18. The Matrix
    19. Forrest Gump
    20. City of God
         

Every single time I run the program and console.log the contents I get the same error

(function (exports, require, module, __filename, __dirname) { 1. The Shawshank Redemption

                                                              ^^^

SyntaxError: Unexpected identifier

    at Object.exports.runInThisContext (vm.js:76:16)

    at Module._pile (module.js:542:28)

    at Object.Module._extensions..js (module.js:579:10)

    at Module.load (module.js:487:32)

    at tryModuleLoad (module.js:446:12)

    at Function.Module._load (module.js:438:3)

    at Module.require (module.js:497:17)

    at require (internal/module.js:20:19)

I will appreciate the feedback on what I am doing wrong, thank you

I'm a beginner in node, I want to create a list data structure from reading a txt file, here is my code to read the file.

        var fs = require("fs");
        var filename = require("../films.txt");
        
        fs.readFile(filename, 'utf8', function(err,data){
          var contents = data;
          var splitContents = contents.split("\n");
          var string = JSON.stringify(splitContents);
          console.log(string)
        });
    
    

And below this line is the text file I want to read is 20 movies, the name of the file is films.txt.

    1. The Shawshank Redemption
    2. The Godfather
    3. The Godfather: Part II
    4. Pulp Fiction
    5. The Good, the Bad and the Ugly
    6. 12 Angry Men
    7. Schindler’s List
    8. The Dark Knight
    9. The Lord of the Rings: The Return of the King
    10. Fight Club
    11. Star Wars: Episode V - The Empire Strikes Back
    12. One Flew Over the Cuckoo’s Nest
    13. The Lord of the Rings: The Fellowship of the Ring
    14. Inception
    15. Goodfellas
    16. Star Wars
    17. Seven Samurai
    18. The Matrix
    19. Forrest Gump
    20. City of God
         

Every single time I run the program and console.log the contents I get the same error

(function (exports, require, module, __filename, __dirname) { 1. The Shawshank Redemption

                                                              ^^^

SyntaxError: Unexpected identifier

    at Object.exports.runInThisContext (vm.js:76:16)

    at Module._pile (module.js:542:28)

    at Object.Module._extensions..js (module.js:579:10)

    at Module.load (module.js:487:32)

    at tryModuleLoad (module.js:446:12)

    at Function.Module._load (module.js:438:3)

    at Module.require (module.js:497:17)

    at require (internal/module.js:20:19)

I will appreciate the feedback on what I am doing wrong, thank you

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 2, 2017 at 21:44 hjmhjm 1,9532 gold badges18 silver badges27 bronze badges 1
  • When you see _pile in the runstack it indicates a syntax issue, meaning your javascript isn't properly formed (missing bracket etc). – rasmeister Commented Feb 2, 2017 at 21:46
Add a ment  | 

2 Answers 2

Reset to default 6

This:

var filename = require("../films.txt");

should be this:

var filename = "../films.txt";

filename should be the path to your file. require are used to load modules (which are valid javascript code not movie list).

require loads a file as if it is Javascript code, attempting to run it. Your text file is not Javascript code. You simply want to do this:

    var fs = require("fs");
    var filename = "../films.txt";

    fs.readFile(filename, 'utf8', function(err,data){
      // ...
Post a comment

comment list (0)

  1. No comments so far