DEV Community

Cover image for Let's create an NPX command
Giuliano1993
Giuliano1993

Posted on

Let's create an NPX command

Good morning everyone and happy MonDEV! ☕

This week I'm here with a question, or rather a proposal, if you've never done it: have you ever created an executable command with npx?
Last week, I was developing some front-end components for Vue.js, and, partly out of laziness and partly out of habit from using Laravel's Artisan command-line tool, which allows generating a boilerplate for almost anything, I wondered if there was something similar for Vue.js. Not finding anything like that, I thought of taking the opportunity to try something new, namely creating an npx command to automate this (albeit brief) process. After struggling a bit, I managed to come up with a command that did exactly what I needed; needless to say, regardless how simple the task was, the satisfaction was great! Have you ever tried it? As a suggestion for the week, I propose you give it a try, with code of your choice. Below are just a few tips:

How to publish on npm

If you haven't already, create an account on npm, so you can publish your packages. After that, in your terminal, you'll need to run the command npm login. Now you're ready, navigate to your project folder and run the npm publish command.

Package.json

In your package.json, you'll need to consider a couple of things:

  1. The value of "version" should be updated every time you run the npm publish command; otherwise, you'll get a version incompatibility error.
  2. In the simplest case, you'll have only one entry point for your command: pass the path to the file to be executed as the value for the bin key.
  3. If you want the GitHub repository link to appear on the npm page, add the repository key and give the repository URL as the value.
Working with FileSystem

If, like me, you choose a project that works with the filesystem, remember that npx downloads the package to a temporary folder on your PC (different depending on your operating system), not in the folder from which the command is launched; keep this in mind when working with paths!


If you want to take a look or try using the command I developed, here's the link to the repo with the documentation; of course, any feedback is welcome 😊. If you take on the challenge and develop even a small command, write to me; I'm curious to see the ideas that come out. I'll be waiting for you in the comments on dev.to 😃 . As always, I hope these tips can be useful to you and have given you a good starting point for a small personal project to take on this week!
I just have to wish you a good day, Happy Coding 1_0!

Top comments (0)