Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify with require('fs')

I was trying to use browserify on a file that uses the fs object. When I browserify it, the call to require('fs') doesn't get transformed and require returns {}.

Is there any workaround for this? I've seen some suggestions on stackoverlow and elsewhere, but none seem to be fully realized.

I actually hoped to create a google web packaged app using browserify for a class I teach.

Thanks in advance.

like image 710
Fred Finkle Avatar asked May 19 '13 22:05

Fred Finkle


People also ask

What is Browserify used for?

Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser.

What is require FS JavaScript?

js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs');

What is Browserify in react js?

This is a browserify transform which works similarly to react-hot-loader. Once you run you app in the browser, it monitors your JavaScript code and only updates the changed component, preserving the state of the application.


1 Answers

If you want to inline file contents from fs.readFileSync() calls, you can use brfs:

var fs = require('fs'); var src = fs.readFileSync(__dirname + '/file.txt'); 

then do:

browserify -t brfs main.js > bundle.js 

and src will be set to the contents of file.txt at compile time.

like image 120
substack Avatar answered Sep 21 '22 17:09

substack