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?
Yes, and yes. Node and Apache / PHP can co-exist on a single server. The only issue you are likely to run into is that they cannot both listen on the same port. HTTP, by default, runs on port 80 and only one process can "listen" on a single port at any one time.
Nope PHP cannot be fully replaced. Yes node js is better but in some cases PHP is still very helpful and better choice like making single forms and small projects.
js and PHP directly, Node. js is much faster than PHP in execution.
It's clear that Node. js is superior in terms of speed, while PHP has greater support and resources. Although it's crucial to choose the language that best fits your project, you should remember that they serve the same purpose in the end.
Edit December 2020
Yarn2 did release a feature called Plug'n'Play which seems to mimic PHP's autoloader: https://yarnpkg.com/features/pnp
It is known to have issues with some packages but I have not tested it myself.
The short answer is: No
For more details, continue reading:
There are two major challenges around the current way require
or import
currently work:
While PHP seems to have developed its own standard and is a bit in its own league, even if someone would develop an equivalent solution to achieve the same for Node.js/JavaScript, we would still need good IDE support. To get good IDE support, this type of change would either:
require
or import
changes that can support absolute paths)There are several answers here (https://gist.github.com/branneman/8048520) and they all seem to break IDE support (I only tested with WebStorm):
require
to support /
: Breaks IDE support when renaming/refactoring.Overall, given that IDE support take precedence over code readability, it looks like there is no good way to implement changes to the current dependency management using Node.js without having the community behind such change.
While not exactly like PHP, it is similar and very handy. I like this package. It is a bit older, but definitely in the right direction.
https://github.com/Specla/Autoloader
Then for database models if you are using Sequelize like I am it is pretty good. https://github.com/boxsnake/sequelize-autoload
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With