NPM package 'bin' script for Windows
Asked Answered
O

3

48

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.

Olimpia answered 1/5, 2012 at 10:16 Comment(0)
J
71

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

Jacquelinejacquelyn answered 1/5, 2012 at 13:44 Comment(6)
The issue was the binary script name ending with a ".js" suffix. NPM creates both a unix-friendly cucumber.js and a cucumber.js.cmd windows-friendly binaries based on the "bin" configuration instruction. Because of how Windows handles file "extensions", when one typed node_modules\.bin\cucumber.js it was running the .js file through JScript instead of the .cmd file. Thanks for the pedantic postscript ;)Olimpia
I ran into exactly this same issue. I'm trying to find a solution that doesn't require me telling Windows users to type a different command. The JavaScript files pre-empting the npm-generated .cmd are executed in Windows Script Host; I'm hoping there's a way to leverage that to proxy to the Node-powered CLI: #24113591Commemorative
how do i make it execute by nodejs instead of microsoft jscript? even changing it to node ./index.js doesnt make it work. correction: re executing npm link seemd to workHoule
I have the same problem as you with error ENOENT: no such file or directory, chmod 'C:\Users\<user>\AppData\Roaming\npm\node_modules\<module>\node .\index.js' . And in package.json "bin": { "<module>": "node ./index.js"} How did you solve it ?Tejada
Ok I added back #!/usr/bin/env nodeeven if it's for windows and it's working.Tejada
@Tejada yep, that did the trick for me too thanks!Roberto
B
5

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"
  }
}
Boatyard answered 26/10, 2012 at 17:9 Comment(0)
C
0

I managed to figure out a solution to a similar issue.

My original plan was to have only one large .js file, for both the API and CLI (the reason is because I didn't know how to share variables between two files at the time). And when everything was built, I tried to add the #!/usr/bin/env node shebang to my file. However that didn't stop Windows Script Host from giving an error.

What I ended up doing was coming up with an idea of a "variable bridge" that allowed variables to be read and set using getVar and setVar. This made me have to extract the CLI code from the API code and add some imports to the variable bridge.

In the CLI file, I added the shebang, and modified the package.json of my project to have:

{
    ...
    "main": "./bin/api.js",
    "bin": {
        "validator": "./bin/cli.js"
    }
    ...
}

Here are a few small notes that I think might help if Windows Script Host is still giving an error (I applied all of them so I'm not sure which one helped):

  • Using only LF line endings seemed to help.

  • It seems that ./bin is the preferred directory for compiled files. I did try ./dist but it didn't work for me.

  • An empty line after the shebang may be needed:

    // cli.js
    
    #!/usr/bin/env node
    
    // code...
    
  • Using the same name for main and bin in package.json seemed to be an issue for me.

Calhoun answered 23/1, 2021 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.