Introducing Project-Level Node.js – NodePHP.

Node.js is an open-source, cross-platform JavaScript run-time environment. Node.js originally written by Ryan Dahl executes JavaScript code outside of a browser. It comes up with a package manager named as NPM (short for Node.js Package Manager). It’s the default package manager for the JavaScript runtime environment Node.js. NPM has an online database of public and paid-for private packages, called the NPM registry. The registry is accessed via the client, and the available packages can be browsed and searched via the NPM website. The package manager and the registry are managed by NPM, Inc.

There are times when you have to execute the Node.js or NPM commands inside the server but not having these platforms already installed in the server restricts you from running the commands. Therefore, I have developed a library (Project-Level Node.js – NodePHP) that can integrate with any PHP Project to install Node.js locally in the project and binds itself with the local installation of the Node.js in the project if it already exists. On top of it, this library provides a Nice PHP-Class interface for the Node.js, NPM and NPX command so you execute the commands using your PHP files itself.

Installation

NodePHP is available on Packagist and installation via Composer is the recommended way to install NodePHP. If you don’t have Composer installed in your system or server then go and check out our Project-Level Composer library or simply copy and run the command below in the terminal.

composer require abhishek6262/nodephp

Usage

Composer makes it really easy to manage the dependencies. Therefore, all we need to do is require the Composer’s autoload file and setup the environment and call the modules we want to execute.

<?php

require_once "vendor/autoload.php";

// $environment = new \abhishek6262\NodePHP\System\Environment('pathToRootDirectory', 'pathToBinDirectory');
$environment = new \abhishek6262\NodePHP\System\Environment(__DIR__);

$node = new \abhishek6262\NodePHP\Node($environment);
$npm  = new \abhishek6262\NodePHP\NPM($environment);
$npx  = new \abhishek6262\NodePHP\NPX($environment);

Install Node.js, NPM or NPX

You can always verify whether the Node.js, NPM or NPX exists through calling the exists() method on the respective class instance and $node->install() method if they’re not already installed.

<?php

$node = new \abhishek6262\NodePHP\Node($environment);
$npm  = new \abhishek6262\NodePHP\NPM($environment);

if (! $node->exists() || ! $npm->exists()) {
    $node->install();
}

Install NPM Packages

You will always need packages for your application to make your life easier and there’s a great slogan, “Don’t reinvent the wheel”. You don’t have to solve the problem which the person has already solved. Thus, NodePHP realizes it and provides an easy-to-go implementation for it. NodePHP can verify whether the packages exists in the Package.json file and have been installed in the project already. To install the packages simply run the $npm->installPackages() command that returns the instance of terminal response with the output and status code so you can validate whether the command ran successfully.

<?php

$npm = new \abhishek6262\NodePHP\NPM($environment);

if ($npm->packagesExists() && ! $npm->packagesInstalled()) {
    $response = $npm->installPackages();
    
    if ($response->statusCode() == '0') {
        echo "Packages successfully installed.";
    } else {
        echo "Failed to install the packages.";
    }
    
    print_r($response->output());
}

Additional Commands

Every NodePHP module comes with an extra method rawCommand($command) that takes the command as an argument to run the command on the respective module whose methods are not yet available in the library.

<?php

$npm = new \abhishek6262\NodePHP\NPM($environment);

$response = $npm->rawCommand('-v');

print_r($response->output());

Conclusion

The Project-Level Node.js library is a salt you can add on to the project depending on your taste if the hosting provider doesn’t already serve you a ready to perfect environment for your application. This library takes away all the pain of manually having to run commands, managing packages, etc. by providing you anice and clean PHP interface so you can store all the procedure and run that single file rather than having to do the commands one by one.

That’s it for Node.js and if you’re looking for something similar for Composer then go and check out our Project-Level Composer library. Finally, let me know about your experience with NodePHP library in the comments below. Signing-Off.