Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-Like Autoloader using Node.js

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?

like image 261
Nicolas Bouvrette Avatar asked Aug 27 '18 21:08

Nicolas Bouvrette


People also ask

CAN node JS and PHP be used together?

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.

Can I replace PHP with node js?

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.

Which is faster node JS or PHP?

js and PHP directly, Node. js is much faster than PHP in execution.

Is PHP 8 faster than node JS?

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.


2 Answers

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:

  1. Relative paths are hard to read and can become confusing when using files with the same name.
  2. Developers must heavily rely on IDEs to refactor their code or to find where a file is when inside another file.

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:

  1. Need to be transparent and integrate to the way IDEs currently work.
  2. Be a change that is driven by the community itself (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):

  1. Using aliases or prepending the path with variables: Breaks IDE support for autocomplete and renaming/refactoring.
  2. Using NODE_PATH as root path: Breaks IDE support for autocomplete and renaming/refactoring.
  3. Wrapping require to support /: Breaks IDE support when renaming/refactoring.
  4. 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.

like image 185
Nicolas Bouvrette Avatar answered Sep 25 '22 23:09

Nicolas Bouvrette


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

like image 38
Goddard Avatar answered Sep 26 '22 23:09

Goddard