Today, we’re going to cover how to turn your Node.js script into a native command-line utility, and then how to share it with the world using NPM (Node Package Manager). In the video, I’m using the trial-and-error approach to show you everything that is required and show the errors you might encounter along the way if you forget a step. It’s actually quite simple.
Cheat Sheet
Turn your Node script into a native command-line utility
- Wrap your Node script in a package by putting it in its own directory and creating the
package.jsonfile (nameandversionare the only required properties) - Expose your script as a command-line utility by adding the
binproperty to thepackage.jsonfile, mapping the command-line utility’s name to your script - In your script, add the shebang:
#!/usr/bin/env node npm linkto install the package globally
Share it with the world
- In the
package.jsonfile, add thepreferGlobalproperty set totrue. npm addusernpm publish
Unshare it with the world
npm unpublish mypackage --force
Example package.json file (minimal)
-
{
-
“name”: “mypackage”,
-
“version”: “0.0.1”,
-
“preferGlobal”: “true”,
-
“bin”: {
-
“mycommand”: “./relative/path/to/script.js”
-
}
-
}


