brfs: fs is not defined error when readFileSync is passed a path variable

I’m getting the following error when I run this with the brfs transform.

var temp = fs.readFileSync(template_path, 'utf8');
           ^
ReferenceError: fs is not defined
...

Test 1:

var fs  = require('fs');
var temp = fs.readFileSync('./templates/test.html', 'utf8');
console.log(temp);

If I run this with node test.js, or

browserify test.js -o output.js -t brfs
node output.js

I get the expected ‘hello world’ content from test.html output to the console. The contents of output.js looks like this:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

/* Test 1 */
var temp = "hello world";
console.log(temp);

},{}]},{},[1]);

However if I do the same with Test 2:

var fs  = require('fs');
var template_path = './templates/test.html';
var temp = fs.readFileSync(template_path, 'utf8');
console.log(temp);

node test.js works fine but running browserify and then node output.js gives me the fs is not defined error. Also output now looks like this:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

/* Test 2 */

var template_path = './templates/test.html';
var temp = fs.readFileSync(template_path, 'utf8');
console.log(temp);

},{}]},{},[1]);

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 37 (3 by maintainers)

Commits related to this issue

Most upvoted comments

It seems that the transform for fs.readFileSync() only works when a string is the first parameter.

Following code doesn’t work

var fs = require('fs');
var filename = '/tmp/hey.txt';

var text = fs.readFileSync(filename, 'utf8');

But this works

var fs = require('fs');

var text = fs.readFileSync('/tmp/hey.txt', 'utf8');

Hello Guys, I am facing a similar problem I have imported “fs” using import * as fs from “fs” and trying to execute fs.readFile() and fs.readFileSync with there respective arguments. But, getting an error as “fs” object doesn’t have a function readFile() and readFileSync(). I have tried using var fsFile = require(“fs”);.

I am using in Cypress automation project in typescript.

I can see how this is a limitation now.

It would be pretty cool to be able to do something like this though:

var dir = fs.readdirSync('./_includes');
for (var i in dir) {
  var file = dir[i];
  var filename = file.split('.')[0];
  var path = './_includes/'+ file;
  temps[filename] = fs.readFileSync(path, 'utf8');
}

But the only way I can see that working is that it turns fs.readdirSync and fs.readFileSync into arrays or objects that hold the files content with the file names as keys.

@elmurphy that code looks expected: in browserify, the fs module is an empty object at runtime. It is impossible to use fs in the browser because there is no way to access the user’s file system. The brfs package can transform calls to specific fs functions only at build time.

var fs = require('fs'); Is compulsory at the beginning of the code Also, the first argument must be a string. Not a concatenated variable, not a variable. It must completely be string.

I can confirm this as well.