question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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]);

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:37 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
brunopscommented, Oct 28, 2014

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');
5reactions
Prashant-Kancommented, Dec 5, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nodejs fs is not defined - Stack Overflow
When attempting to use fs.readFileSync with the files array in the code below. var fs = require('fs'); var path = require('path'); var ...
Read more >
Node.js v19.3.0 Documentation
Iterates through the list of functions passed to tracker.calls() and will throw an error for functions that have not been called the expected...
Read more >
Error handling in Node.js - LogRocket Blog
Deliver reliable software by learning how to handle errors in Node.js using callbacks, promises, and event emitters.
Read more >
fs-extra - npm
fs -extra contains methods that aren't included in the vanilla Node.js fs package. Such as recursive mkdir, copy, and remove.
Read more >
Relative fs.readFileSync paths with Node.js - Ultimate Courses
In this post you'll learn how to use fs.readFileSync to give you a relative path to your asset. By default, that's not the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found