Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impossible to use correctly the node module 'fs' : get the error "fs.readdir is not a function" #872

Open
spacecodeur opened this issue Dec 5, 2020 · 2 comments

Comments

@spacecodeur
Copy link

I work on Symfony 5, from "node side" I have a main javascirpt "main.js" and, in the same directory, a directory called "modules" that contains some javascript Script (they are Class).

From the "main,js" I instantiate manually all the modules class like this :

import {Module1} from "./modules/Module1";
import {Module2} from "./modules/Module2";
// ...
import {Module2} from "./modules/ModuleN";

function loadModules(){
    return [
        new module1(),
        new module2(),
        // ...
        new moduleN(),
    ];
}

Now I'm looking for instantiate automatically all modules files.

So I must get all files names in the directory 'modules' and do something like this :

function loadModules(){
        const fs = require('fs');
        fs.readdir('./modules', function (files){
            files.forEach(function(file){
                // ...
            });
        });
}

But when I use the readdir method of "fs", I get this error in front side : fs.readdir is not a function

I followed this solution webpack-contrib/css-loader#447 (comment) , but I keep the same error message

Any help is welcome :)

@Lyrkan
Copy link
Collaborator

Lyrkan commented Dec 9, 2020

Hi @kegilko,

I'm not sure what you mean by "node side" but unless you're building a Node.js app you probably don't want to use fs.

A good way to achieve what you describe would be to use require.context which would then be interpreted by Webpack at build time.

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants