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
- Use the value for remote.path directly in fs.readFileSync There is a bug in brfs which disallows passing variables to fs.readFileSync. This means that anyone using this module with Browserify will ha... — committed to RichardLitt/regenerator by RichardLitt 9 years ago
- Don't rely on fs.readdirSync to load schemas Browserify/brfs can't handle this properly as described in https://github.com/substack/brfs/issues/36 and we want to use this module in the lr-sense-follo... — committed to eHealthAfrica/data-models-deprecated by rmehner 10 years ago
- fix: path.join must be inline for brfs to work see https://github.com/substack/brfs/issues/36 — committed to hypermodules/entypo by ungoldman 8 years ago
It seems that the transform for fs.readFileSync() only works when a string is the first parameter.
Following code doesn’t work
But this works
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:
But the only way I can see that working is that it turns
fs.readdirSyncandfs.readFileSyncinto arrays or objects that hold the files content with the file names as keys.@elmurphy that code looks expected: in browserify, the
fsmodule is an empty object at runtime. It is impossible to usefsin the browser because there is no way to access the user’s file system. Thebrfspackage can transform calls to specificfsfunctions 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.