fs.readFileSync is not a function?
You can not use "fs" in browsers because it is a Node.js specific library that enables file system operations on OS level. Browser do not support these low-level APIs so you can unfortunately only use it in Node.js.
Google currently develops a browser based file system api, maybe this helps.
Btw Selenium will also not work because it spawns another browser again with OS level APIs.
Why doesn't Browserify work with fs.readFileSync?
From Browserify:
Browsers don't have the require method defined, but Node.js does. With Browserify you can write code that uses require in the same way that you would use it in Node.
Browserify let's you use 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.
Uncaught TypeError: fs.readFileSync is not a function in console
NOTE: fs
is a nodejs module, you cannot use it in Browser.
Import the fs module,
readFileSync
will provide you the Buffer
to use the split()
function you have to convert the Buffer
into String
var fs = require('fs')
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8') // converting the Buffer into String
var textByLine = string.split("\n")
console.log(textByLine);
▼ UPDATE ▼
Server-Side
fs
is a nodejs built-in module, you cannot use it in Browser(Client-Side). Use fs
in server-side to do the manipulation, get the data and format in required type, then you can render
it with html
, ejs
many more.. templating engines
Here i have created a Nodejs Server using express, and from the browser hit the http://localhost:8000/
you will get the Array of Data
You can format your data and render it with the .ejs
or html
files using res.render
app.js
var express = require('express');
var app = express();
var fs = require('fs')
app.get('/', function (request, response) {
var text = fs.readFileSync("./men.text");
var string = text.toString('utf-8')
var textByLine = string.split("\n")
console.log(textByLine);
response.send(textByLine);
});
app.listen('8000');
Dummy Output:
readFileSync is not a function
Node.js does not use Require.js. Require.js was built so that you could have asynchronous module loading on the client-side (in your browser).
Node.js uses CommonJS style modules. Your code using CommonJS would look like this:
var fs = require('fs');
console.log("\n *STARTING* \n");
var contents = fs.readFileSync("sliderImages", "utf8");
If we assume you saved this in a file called main.js
you would then enter this command in your console (make sure you are in the same directory as the file):
node main.js
This code will not run in the browser. Node.js runs on the server. If you want to load a JSON file on the browser side then you'll need to load it using AJAX. There are numerous resources available to show you how to do this. Be aware that you must either run your page from a server or have a special flag enabled to load in files from the file system.
Browserify with require('fs')
Which filesystem should the browser use then? The HTML5 filesystem is not really comparable to a traditional filesystem. It doesn't have symlinks, and it is only accessible asynchronously outside Web Workers.
So the answer is: Write an abstraction layer yourself that can rely on the fs module when running in Node.js, and the HTML5 FS API when running in the browser. The differences are too large to have browserify translate for you.
PDFKit - Custom Fonts - fs.readFileSync is not a function
It seems you must use Browserify for this functionality and that using a pre-compiled PDFKit.js won't cut it for any of the Node.js-specific functionality.
Related Topics
How to Detect Browser Refresh Using Angular
How to Format Base64 Encoded String While Sending in Json
Save Embedded Pdf from Website
Check Whether an Input String Contains a Number in JavaScript
How to Check If Input File Is Empty in Jquery
Cannot Access Json Object Property Returns Undefined
Concatenate Json to a String in JavaScript
How to Set Image to Fit Width of the Page Using Jspdf
Stylesheet Not Loading Because Mime Type Is Text/Html
How to Apply Count Up Function to Multiple Values on the Page
Google Script to See If Text Contains a Value
Close Window Automatically After Printing Dialog Closes
Image Resizing Client-Side With JavaScript Before Upload to the Server
Find Minimum Numbers of Currency for Required Amount
How to Access Session Variables and Set Them in JavaScript
Regex for Names Validation Allow Only Letters and Spaces