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
2 Answers
Reset to default 6This:
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){
// ...