Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

node.js - 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.

question from:https://stackoverflow.com/questions/10396305/npm-package-bin-script-for-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...