简体   繁体   English

为什么 Browserify 不能与 fs.readFileSync 一起使用?

[英]Why doesn't Browserify work with fs.readFileSync?

When I run my code:当我运行我的代码时:

var fs = require('fs');
var text = fs.readFileSync('data.txt').toString().split("\n");

function getDataJS(){
    var i = 0;
    var c = "";
    var d = "";
    var t = "";
    var r = "";
    for(i = 0;i<text.length;i++){
        if(i == 0){
            c = text[i];
            document.getElementById('con').innerHTML = c;
        } else if(i == 1){
            d = text[i]
            document.getElementById('dec').innerHTML = d;
        } else if(i == 2){
            t = text[i]
            document.getElementById('tes').innerHTML = t;
        } else if(i == 3){
            r = text[i]
            document.getElementById('rec').innerHTML = r;
        }
    }
}


module.exports = getDataJS();

(with my html) I get: (用我的html)我得到:

Uncaught TypeError: fs.readFileSync is not a function

Does anyone know why this is?有人知道为什么吗? I know that node does not work with browsers, but I'm confused why browserify isn't working.我知道该节点不适用于浏览器,但我很困惑为什么 browserify 不起作用。

From Browserify :Browserify

Browsers don't have the require method defined, but Node.js does.浏览器没有定义 require 方法,但 Node.js 有。 With Browserify you can write code that uses require in the same way that you would use it in Node.使用 Browserify,您可以编写使用 require 的代码,就像在 Node.js 中使用它一样。


Browserify let's you use require . Browserify 让你使用require It doesn't let you use the APIs provided by Node.js that are not provided by browsers… including those needed to read files from the computer the code is running on.不允许您使用浏览器未提供的 Node.js 提供的 API……包括从运行代码的计算机读取文件所需的 API。

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

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