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.

support fs.readdir and fs.readdirSync

See original GitHub issue

I think this is feasible, and I personally think this is really useful.

In node.js it’s reasonably common for me to do things like:

// controllers/index.js
'use strict';
var fs = require('fs');
var path = require('path');

var ROOT_PATH = __dirname;

module.exports = function(path) {
  var fnames = fs.readdirSync(ROOT_PATH);

  var paths = fnames.map(function(fname) {
    return path.join(ROOT_PATH, fname, '.js');
  });

  var modules = paths.map(function(path) {
    return require(path);
  });

  return modules.map(function(module) {
    // do something with the module
    // i.e. if it's this is a mongoose schema bootstrapping utility, register the model.
    //       if this is a express controller bootstrapping utility, parse its methods and register
   //        matching routes.
   //        etc.
  });
};

This allows us to abstract whole parts of the code, later on - even treat them as completely separate modules:

// app.js
var controllers = require('./controllers')

// bootstrap the controllers
controllers(/* something would come here such as an express app or a mongoose.Connection */);

If we could do this with browserify, things like bootstrapping an Ember app, could be automated. I know there are a couple of problems.

Here’s an analogous example that works, though not exactly as one would hope it’d.

I know this has to do with limitations on the browserify module (which may be impossible to address) but this would be really cool.

Perhaps we could even parse things like:

['./a', './b'].map(function(name) {
  return require(name);
});

Into:

[require('./a'), require('./b')];

Or provide an utility requireMap thing that resolved into that.

I don’t know if this is a relevant issue. But I though this would be a nice thing, if it was possible.

At it’s core, the main point was simply to add:

var fs = require('fs');
var files = fs.readdirSync('.');

To be transformed into:

var fs = require('fs');
var files = ['a.js', 'b.js', 'main.js' /* etc. */];

What do you think?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:9

github_iconTop GitHub Comments

4reactions
callumlockecommented, Apr 21, 2015

it would be cool if there was some way this could work:

var fs = require('fs');

var templates = {};

fs.readdirSync(__dirname + '/../templates').forEach(function (filename) {
  templates[filename] = fs.readFileSync(__dirname + '/../templates/' + filename, 'utf-8');
});
0reactions
cancerberoSgxcommented, Jul 1, 2018

@callumlocke finally made it and working fine! https://github.com/cancerberoSgx/br-fs-to-json packing a glob of files is what I always wanted and since you will be requiring a “strange” library fs-to-json I make sure it doesn’t contaminate your project with its dependencies. Much more practical than brfs IMO

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js fs.readdirSync() Method - GeeksforGeeks
The fs.readdirSync() method is used to synchronously read the contents of a given directory. The method returns an array with all the file ......
Read more >
File system | Node.js v19.3.0 Documentation
The fs/promises API provides asynchronous file system methods that return promises. The promise APIs use the underlying Node.js threadpool to perform file ......
Read more >
fs.readdirSync JavaScript and Node.js code examples - Tabnine
Best JavaScript code snippets using fs.readdirSync(Showing top 15 results out of 4,050) ; fs.readdirSync(inputFolder).forEach(function (dirContent) { dirContent ...
Read more >
Node fs.readdirsync read directory in npm package
readdirSync and it work perfectly, but when i publish to npm and install this package to my new project, it not read my...
Read more >
Enhanced fs.readdir - JavaScript Dev Tools
Fully backward-compatible drop-in replacement for fs.readdir() and fs.readdirSync(). Can crawl sub-directories - you can even control which ones. Supports ...
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