I'm going to talk to you about the npx tool, which is a standard NodeJS tool. I didn't really know this tool before, but now I love it; it saves me a lot of time!
I use it to easily deploy my NodeJS scripts to my servers and run them to patch data. Maybe you're familiar with this: you make a mistake, then you run a patch to try and fix it... okay, it happens to me too... sometimes :-)
What is npx?
npx is a tool that allows you to download and run a NodeJS package. If I use npx myPackageName, it will automatically download and run the NodeJS package myPackageName.
Run a NodeJS package?
In package.json there is an entry named bin which allows you to specify the name of a script that will be executed by npx.
{
"name': "myPackageName",
"version": "1.0.1",
"bin": "./myScript.js"
}
When I do npx myPackageName arg1 arg2, it downloads the latest version of the package myPackageName and then executes the script ./myScript.js with the arguments arg1 and arg2. It's the same as if I were doing node ./myScript.js -- arg1 arg2.
So it's very practical!
The only small thing is that you need to add this line at the beginning of the script (a shebang): #!/usr/bin/env node. So at the beginning of my myScript.js file, I need to add this line. That's all!
What I used to do for my patches
Before discovering npx, I had scripts that I uploaded to my servers (zip, upload, unzip) and then ran manually. Then, when I updated these scripts, I had to upload them again to all my servers before running them.
With npx, there's no need to do that anymore! Because it automatically downloads the latest version of my package!
Creating a private repository is super easy!
I use the Verdaccio tool, which allows me to have private NodeJS packages.
This lets me create private packages that I can deploy very quickly on my servers, without errors, while also having a history of my previous versions.
When you publish a package, it's visible to everyone. But with a tool like Verdaccio, you can easily have a private repository.
Okay, I'm going to make you smile... npx verdaccio... there you go, it's installed on your computer! All you have to do is open the URL http://myPrivateNpmServer:4873/ in your browser to see for yourself!
The Verdaccio welcome screen will tell you what to do to create a login/password. It's very simple!
Once that's done, you just need to do this on your servers so they use Verdaccio: npm set registry http://myPrivateNpmServer:4873/.
That's it! You now have your own private repository!
Complete Example
Here I'm going to show you a complete project that you can copy and paste.
package.json
{
"name": "myPatchName",
"version": "1.0.0,
"scripts: {
"publish": "npm publish --registry http://myPrivateNpmServer:4873/"
},
"bin": "./myScript.js"
}
myScript.js
#!/usr/bin/env node
console.log("Executing my patch!");
And that's it!
To debug the script, simply run
npx myPatchNamein the local folder. It will automatically use your development version.To publish the script, simply run
npm run publishin the local folder.
Top comments (0)