I'm slowly transitioning from PHP to Node.js and was trying to find something similar to composer dumpautoload
. Thanks to PSR-4, it's easy to get access to any class in any file in PHP when using this command with simple use
statements at the beginning of each file.
npm
seems to do a great job managing packages and dependencies but having the same flexibility within your own project would avoid creating require
statements that can easily break if a file changes path.
Example of what I would be looking for - 2 files in the same folder:
Some testClass.js (class file)
var testClass = {
sayHello: function () {
console.log('this is a test');
}
};
module.exports = testClass ;
Normally this is what you would put in another file index.js file:
var testClass = require('./testClass');
testClass.sayHello();
But imagine you could pre-index all your classes with some app or command (like PHP's composer dumpautoload
and simply run this:
var testClass = require('testClass');
testClass.sayHello();
I couldn't find any solution that seems to achieve this.
Did I miss something?