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 ?
– Pedro Neri
at first, you should use
require
and notimport
, i and..:var fs = require('fs');
- you could edit your question and include the link to the tutorial/video?– Daniel
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?– MagicHat
What version of Node are you using? Maybe you still don’t support
import
. Upgrade to version 8.– Sergio
@Pedroneri through the Node terminal.
– Diemano Nóbrega
@blogger I added the video I mentioned in the question.
– Diemano Nóbrega
@Magichat I want to treat a 'txt' file that is in the local folder through functions until it turns into a json.
– Diemano Nóbrega
I took a look at the video - it cites the use of
require
the moment he writes theimport
(I would try to do "the old-fashioned way" and userequire
- you did it? ) - Anyway, he seems also be wearing Babel that "transpires" the code and allows the use ofimport
when it is not available– Daniel
@blogger I used require and it worked ;) but I’d like to understand how he did it that way, anyway thanks for the help!
– Diemano Nóbrega
Post an answer to the problem so the question doesn’t get "lost" around as "unanswered question", and then mark it as "correct answer".
– Sam
Nodejs by default uses require instead of import. const Fs = require('Fs')
– ayelsew
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!
– Sorack