fs is not defined error when readFileSync is passed a path variable
See original GitHub issueI’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:
- Created 9 years ago
- Comments:37 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.