How to read a local txt file in Javascript

Asked

Viewed 882 times

4

Hello I’m a beginner in Javascript and I’m watching some videos on youtube about reduce and my question is, in the video it puts a code at the top of the file.

import fs from 'fs'

But when I put this code in my js and try to compile (Node filename.js) it gives error in the compilation:

(function (exports, require, module, __filename, __dirname) { import fs from 'fs'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)

How should I import this module to handle a txt in my code.

Sorry if it may be something very obvious, but I’m really not getting it.

Video link: https://www.youtube.com/watch?v=1DMolJ2FrNY&list=PL0zVEGEvSaeEd9hlmCXrk5yUyqUag-n84&index=4

  • How you are running this code ?

  • at first, you should use require and not import, i and..: var fs = require('fs'); - you could edit your question and include the link to the tutorial/video?

  • When it says : "How to read a local txt file in Javascript" what do you mean? What would you like to do? What is path of the archive?

  • What version of Node are you using? Maybe you still don’t support import. Upgrade to version 8.

  • @Pedroneri through the Node terminal.

  • @blogger I added the video I mentioned in the question.

  • @Magichat I want to treat a 'txt' file that is in the local folder through functions until it turns into a json.

  • 1

    I took a look at the video - it cites the use of require the moment he writes the import (I would try to do "the old-fashioned way" and use require - you did it? ) - Anyway, he seems also be wearing Babel that "transpires" the code and allows the use of import when it is not available

  • @blogger I used require and it worked ;) but I’d like to understand how he did it that way, anyway thanks for the help!

  • Post an answer to the problem so the question doesn’t get "lost" around as "unanswered question", and then mark it as "correct answer".

  • Nodejs by default uses require instead of import. const Fs = require('Fs')

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

Show 7 more comments

1 answer

2

To request a module on Node use the require:

const fs = require('fs');

And to read the file use the function readFile or readFileSync:

fs.readFile('<diretório>', (err, data) => {
  // => null, <data>
});

fs.readFileSync('<diretório>');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.