PHP-Like Autoloader using Node.js
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
import currently work:
- Relative paths are hard to read and can become confusing when using files with the same name.
- Developers must heavily rely on IDEs to refactor their code or to find where a file is when inside another file.
- Need to be transparent and integrate to the way IDEs currently work.
- Be a change that is driven by the community itself (either
importchanges 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):
- Using aliases or prepending the path with variables: Breaks IDE support for autocomplete and renaming/refactoring.
- Using NODE_PATH as root path: Breaks IDE support for autocomplete and renaming/refactoring.
/: Breaks IDE support when renaming/refactoring.
- Creating a new custom method: Breaks IDE support for autocomplete.
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.
Then for database models if you are using Sequelize like I am it is pretty good. https://github.com/boxsnake/sequelize-autoload