NPM package 'bin' script for Windows

Cucumber.js is supplying a command-line "binary" which is a simple .js file containing a shebang instruction:

#!/usr/bin/env node
var Cucumber = require('../lib/cucumber');
// ...

The binary is specified in package.json with the "bin" configuration key:

{ "name" : "cucumber"
, "description" : "The official JavaScript implementation of Cucumber."
// ...
, "bin": { "cucumber.js": "./bin/cucumber.js" }
// ...

This all works well on POSIX systems. Someone reported an issue when running Cucumber.js on Windows.

Basically, the .js file seems to be executed through the JScript interpreter of Windows (not Node.js) and it throws a syntax error because of the shebang instruction.

My question is: what is the recommended way of setting up a "binary" script that works on both UNIX and Windows systems?

Thanks.


Windows ignores the shebang line #!/usr/bin/env node and will execute it according to the .js file association. Be explicit about calling your script with node

node hello.js

ps. Pedantry: shebangs aren't in the POSIX standard but they are supported by most *nix system.


If you package your project for Npm, use the 'bin' field in package.json. Then on Windows, Npm will install a .cmd wrapper along side your script so users can execute it from the command-line

hello

For npm to create the shim right, the script must have the shebang line #!/usr/bin/env node


your "bin" should be "cucumber" npm will create a "cucumber" or "cucumber.cmd" file pointing to "node %SCRIPTNAME%". the former being for posix environments, the latter being for windows use... If you want the "js" to be part of the executable name... you should use a hyphon instead... "cucumber-js" ... Having a .js file will come before the .js.cmd in your case causing the WScript interpreter to run it as a JScript file, not a node script.

I would suggest looking at coffee-script's package.json for a good example.

{
  "name":         "coffee-script",
  "description":  "Unfancy JavaScript",
  "keywords":     ["javascript", "language", "coffeescript", "compiler"],
  "author":       "Jeremy Ashkenas",
  "version":      "1.4.0",
  "licenses":     [{
    "type":       "MIT",
    "url":        "https://raw.github.com/jashkenas/coffee-script/master/LICENSE"
  }],
  "engines":      {
    "node":       ">=0.4.0"
  },
  "directories" : {
    "lib" : "./lib/coffee-script"
  },
  "main" : "./lib/coffee-script/coffee-script",
  "bin":          {
    "coffee":     "./bin/coffee",
    "cake":       "./bin/cake"
  },
  "scripts": {
    "test": "node ./bin/cake test"
  },
  "homepage":     "http://coffeescript.org",
  "bugs":         "https://github.com/jashkenas/coffee-script/issues",
  "repository":   {
    "type": "git",
    "url": "git://github.com/jashkenas/coffee-script.git"
  },
  "devDependencies": {
    "uglify-js":  ">=1.0.0",
    "jison":      ">=0.2.0"
  }
}