wdkendall
wdkendall

Reputation: 65

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?

Upvotes: 0

Views: 217

Answers (1)

robertklep
robertklep

Reputation: 203574

Running npm install inside a package folder will install its dependencies, but it doesn't install any binaries that the package itself declares (you could argue what the point of that would be).

That only happens when the package is installed as a package (using npm install package-name or as a dependency for other packages).

Upvotes: 1

Related Questions