hsume2 / browserify-rails

Deprecated https://github.com/browserify-rails/browserify-rails
MIT License
57 stars 11 forks source link

What's the correct way to require an entire directory? #32

Closed gabrielecirulli closed 10 years ago

gabrielecirulli commented 10 years ago

This is more of a question than an issue. I am looking for a proper way to require all of the JS files present in a directory.

Here are the approaches I tried:

Error while running `/path/to/rails/app/./node_modules/.bin/browserify -d --list -`:

events.js:85
      throw er; // Unhandled 'error' event
            ^

What's the correct way to do this?

martenlienen commented 10 years ago

Node does not know the concept of mass-inclusion, because it does not make sense there. In contrast to ruby, where required modules modify the global namespace, node modules are executed in a local context and export things explicitly. And these exports are the return value of the require call. What should a mass require return? And if it does not return anything, it is not useful, because requiring is side effect free.

You should rethink your code organization and read up on the node.js module system. If you cannot restructure your code, the node way and in corollary browserify is not right for you.

PS: Of course there are ways, to do all the bad stuff, but I am not going to help you with that.