Jordan Morris Jordan Morris - 1 month ago 4x
Node.js Question

Best practice for distributing a nodejs command line application

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?).

  • 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.