Friday, 7 April 2017

package.json for minimal command-line utility

I am trying to create a command-line utility. But, npm install (no -g) does not link the executable.

My expectation is that npm install would install my executable locally.

My package.json looks like:

{
  "name": "test-bin",
  "version": "0.1.0",
  "description": "Test bin",
  "bin": "./bin/test-bin.js",
  "main": "./index.js",
  "author": "",
  "license": "ISC",
  "repository": {
    "type": "git",
    "url": "file:///tmp/test-bin.git"
  }
}

index.js is:

module.exports = function() {
  console.log('invoked')
}

bin/test-bin.js is:

require('../')()

If I run npm install, node_modules is created, but not .bin

However, if create another project elsewhere that uses the first as a dependency:

{
  "name": "test-test-bin",
  "version": "0.1.0",
  "description": "Test test bin",
  "author": "",
  "license": "ISC",
  "repository": {
    "type": "git",
    "url": "file:///tmp/test-test-bin.git"
  },
  "dependencies": {
    "test-bin": "file:///Users/you/somewhere/test-bin"
  }
}

then npm install links the executable in that project:

node_modules/.bin/test-bin

The npm documentation says, about "bin":

To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.

Is it as designed, or am I missing something?



via wdkendall