cpx — The npx counterpart of the PHP ecosystem
If you’re coming from the JavaScript world or even if you’ve worked with modern JavaScript briefly, you might know the importance of npm and its package ecosystem.
The context
You can install packages from the npm registry and use them in your projects. But sometimes, there might be some packages that you might need to install globally to use them in your terminal. For instance, you need to install Jest globally (or per-project basis as a dev dependency) using npm to run tests on your JavaScript projects.
This is where a tool like npx can simplify this process for you. It’s a CLI tool that allows you to run npm packages without having to install them globally.
npx comes pre-installed in the npm version 5.2.0 and later. So, for instance, if we want to run jest in our project, we can simply run the following command.
npx jest
This will fetch the jest package from the npm registry and run it in the current project. So, you don’t need to install it globally.
The npx of PHP
As the title of this article suggests, we now have a similar tool for PHP. It’s called cpx. The concept here remains the same.
Once installed, it allows you to run Composer packages “on the fly” without having to install them globally.
You can install it globally using the following command.
composer global require cpx/cpx
Now, for instance, if you want to run php-cs-fixer
in your project, you can simply run the following command.
cpx php-cs-fixer fix
This will fetch the php-cs-fixer package from the Composer registry and run it in the current project.
As you can tell, when you run the command, cpx will try to find if the package is available locally, and if not, it will fetch it from the Composer registry and run it.
It also handles the updates of the packages and their dependencies behind the scenes, which is pretty cool.
In closing
I have only started using cpx in some of my personal projects and so far it kind of fits in my workflow. It’s still a young tool but I can see a huge potential in it if it becomes more popular.
If you’re interested in learning more about cpx, you can check out the official documentation and the GitHub repository.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.