Introduction

I’ve found myself creating a lot of tools that I use on a daily basis, and I’ve always wanted to make them accessible from anywhere, without having to download them. I’ve found that the best way to do this is to create an NPX tool.

Setting up the project

First, we need to create a new node project. If you haven’t done this enough already, here’s a refresher:

mkdir my-npx-tool
cd my-npx-tool
npm init -y

Anyway, theres one important thing that makes NPX tools different from any other Node project you’d make. go ahead and open up package.json and add this line:

// ...
"bin": {
    "my-npx-tool": "bin/index.js"
},
// ...

This will tell npm that the file bin/index.js is the entry point for the tool. Go ahead and create a new file at bin/index.js and the following code:

#! /usr/bin/env node
console.log("Hello, World!");

All this file will do is print Hello, World!. However, there is one important thing to note about the following line:

#! /usr/bin/env node

This line should be added to all files that will be executed through the command line. It’s called a Shebang, and it tells the shell what program to use to execute the file. In this case, we’re telling the shell to use Node to execute the file.

Now, if you were to run npx my-npx-tool within this project’s directory, you would see Hello, World! printed to the console. Great!

Publishing the project

Currently, the command we made can only be ran from the same directory as the project. That’s not very useful, so we will need to publish the package to NPM to really make good use of it.

First, you will need to create an NPM account. You can do that on their website, here.

Once we’ve done that, you need to log into NPM in your development environment. You can do that by running npm login in your terminal. You will be prompted to enter your username, password, and email address. Once you’ve done that, you’re ready to publish!

When publishing the package, you can either publish it to your user scope, which will allow you to access it via npx @yourUserName/my-npx-tool or you can publish it to the global scope, which will allow you to access it via npx my-npx-tool. I will be publishing it to the user scope, but you can publish it to the global scope if you’d like. The only caveat is the name of your package must be unique.

If you are going the user scope route, you need to update your package.json to reflect that. Go ahead and run the following command and follow the prompts:

npm init --scope=@yourUserName/my-npx-tool

Once you’ve done that, you can publish the package like so:

npm publish --access=public

If all went well, you should now be able to run npx @yourUserName/my-npx-tool from any machine with NPM installed and see Hello, World! printed to the console!

Conclusion

You can now create NPX tools for any project you want! I’ve gave you the lemons, now make lemonade. If you’d like to see a more complex example, you can check out my post on creating a Typescript project generator or init-ts.