简体   繁体   English

FS 模块读取 CSV 文件

[英]FS module to read CSV file

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' ".我是 node js 的新手,我尝试使用模块“fs”,但我总是遇到我无法理解的相同错误:“找不到模块 '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谢谢

You're using fs wrong!你用fs错了!

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));
});

By the assumption that you are using webpack I don't think that it is possible to require fs in the browser.假设您使用的是 webpack,我认为浏览器中不可能需要 fs。 Webpack take your code and bundle it, then it is "usually" sent to a browser. Webpack 获取您的代码并将其打包,然后“通常”将其发送到浏览器。 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.尽管 webpack 正在读取和使用 node.js 代码,但它最终会在没有所需的“fs”模块且不是 node.js 环境的浏览器中运行。

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.使用 webpack,可以更改配置并将自定义文件作为一个步骤加载。 You should take a look at Adding a csv file to a webpack build您应该查看将 csv 文件添加到 webpack 构建

Or Webpack Express Cannot Resolve Module 'fs', Request Dependency is Expression或者Webpack Express 无法解析模块 'fs',请求依赖是表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM