2

How do people expect a nodejs commandline application to be organised when you distributed it via npm?

Would they expect to have to build it? Install it locally or globally? Should it always output a bin.js? Do you need some kind of alias / script to run it (via bin.js?).

Jordan Morris
  • 2,101
  • 2
  • 24
  • 41
  • 1
    Having written (and used) command line apps before the most common way people expect to be able to install it is via `npm install -g`. You just need to specify the 'bin' section in 'package.json'. There is no need to manually output a bin.js file. – slebetman Aug 24 '16 at 05:39
  • Also, to avoid this question being closed you should rephrase it from "what's the best way to.." to "how do I..". The way it's phrased now imply you're looking to poll opinion - which is strongly discouraged on SO. Asking questions is fine, asking for opinion is not OK. – slebetman Aug 24 '16 at 05:41
  • I want to know about industry convention, not just 'a way', or someone's favourite way – Jordan Morris Aug 24 '16 at 08:50

1 Answers1

6
  • Use the bin field in package.json to specify the entrypoint javascript file for your CLI.
  • Add #!/usr/bin/env node to the top of your entrypoint js file.
  • Specify the files which need to be packaged with your app, using the files field of package.json
  • In README, provide an npm install command line which a consumer can use to install your app, for example it could point at a github repository, or a package uploaded to the npm registry, which you can create with npm pack.

Upon installing the app locally or globally, a script will be created in the path (or locally in node_modules/.bin), which will let your command line app be run conveniently.

Jordan Morris
  • 2,101
  • 2
  • 24
  • 41