0

I'm new with node js and I tried to use the module "fs" but I have always the same error that I can't understand: " Cannot find module 'fs' ". The code is:

     const fs = require('fs');
      fs.createReadStream("data/dataset.csv", "utf8", function(error, data)
        {
          data = d3.tsv.parse(data);
          console.log(JSON.stringify(data));
        }
      );

Can someone help me? Thank you

  • 1
    What version of node are you using `node --version`? Where de code is running (directly run by node with or runned by the browser)? – Plpicard Feb 28 '20 at 19:46
  • I'm using v12.16.1 and I'm running the code by the browser – Luigi Zollo Feb 28 '20 at 20:11
  • Are you running Webpack or Angular or a code bundler? Please provide more information in your question on your setup and the code you are running. Otherwise the question is too vague. – Plpicard Feb 28 '20 at 20:54
  • Yes, I'm using Webpack and I use the command "npm install" in the folder of project. I'm sorry for being inaccurate but I didn't understand very well this technologie. – Luigi Zollo Feb 28 '20 at 21:01
  • Does this answer your question? [Node cannot find module "fs" when using webpack](https://stackoverflow.com/questions/39249237/node-cannot-find-module-fs-when-using-webpack) – Plpicard Feb 28 '20 at 21:03
  • What I have to do? – Luigi Zollo Feb 28 '20 at 21:04

2 Answers2

0

You're using fs wrong!

First of all you're not using stream properly.

Also you don't need stream for this task. (if you just need to read file)

Try this instead:

fs.readFile("data/dataset.csv", "utf8", (err, data) => {
  if (err) throw err;
  data = d3.tsv.parse(data);
  console.log(JSON.stringify(data));
});
Serhiy Mamedov
  • 1,080
  • 5
  • 11
  • thank you but the problem in on the first line of code, still doesn't work – Luigi Zollo Feb 28 '20 at 20:17
  • I've seen your comment: "I'm using v12.16.1 and I'm running the code by the browser – Luigi Zollo" Fs - it's node js module only. You can't run it in the browser. Further more you can't access your system files from browser at all. So try in node environment. – Serhiy Mamedov Feb 28 '20 at 20:22
  • Can you please explain better? Thank you – Luigi Zollo Feb 28 '20 at 20:24
  • Node.js and browser javascript are different things. Just make a file named test.js Put this code there and do " node ./test.js " in the console and it will work. But if you copy and paste it to your browser console it will not work – Serhiy Mamedov Feb 28 '20 at 20:32
  • Ok, so I don't run in the browser because I use "npm run start" command in the project's folder but it still doesn't work – Luigi Zollo Feb 28 '20 at 20:38
0

By the assumption that you are using webpack I don't think that it is possible to require fs in the browser. Webpack take your code and bundle it, then it is "usually" sent to a browser. Although webpack is reading and using node.js code it will ultimately run in the browser which does not have the required 'fs' module and is not a node.js environment.

I am not sure what you are trying to achieve.

With webpack it is possible to change the configuration and have custom file loaded as a step. You should take a look at Adding a csv file to a webpack build

Or Webpack Express Cannot Resolve Module 'fs', Request Dependency is Expression

Plpicard
  • 1,066
  • 6
  • 15
  • Sorry but I still don't understand the problem, I try to be more accurate. I developed a simple app using d3.js and it worked on XAMPP. After, I decided to "move" everything on node js but I had the problem to read a csv file(simple d3 function doesn't work) so I changed the function and I tried to use "fs" module. I in the project folder that I created there are the node_modules folder, package.json and webpack.config.js file. – Luigi Zollo Feb 29 '20 at 07:47
  • The problem seems to come from the understanding of webpack. Webpack can do many things but it primarily load a bunch of file and package them to be ready to serve. To go to production you would run webpack build. The result of this build is a `dist` folder. The content of this folder is what would be in your sftp. From that point its is not node js anymore. This means that you could require the file from an url or you could preload it with webpack. For further help a github link would be useful. Also edit your question to add details. – Plpicard Mar 01 '20 at 19:52