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.json
file (name
andversion
are the only required properties) - Expose your script as a command-line utility by adding the
bin
property to thepackage.json
file, mapping the command-line utility’s name to your script - In your script, add the shebang:
#!/usr/bin/env node
npm link
to install the package globally
Share it with the world
- In the
package.json
file, add thepreferGlobal
property set totrue
. npm adduser
npm publish
Unshare it with the world
npm unpublish mypackage --force
Example package.json file (minimal)
1 | { |