Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 380
jbpros Avatar asked May 01 '12 10:05

jbpros


People also ask

How do I install npm files on Windows 10?

For Node. js projects, the easiest way to install npm packages is through the npm package installation window. To access this window, right-click the npm node in the project and select Install New npm Packages. In this window you can search for a package, specify options, and install.

Can npm run on Windows?

js and NPM installations on Windows are relatively simple. You can install both from a download link. First, go to the Node installation page and download the installer. Choose the operating system you're using, and then go from there.

What is npm start script?

npm start: npm start script is used to execute the defined file in it without typing its execution command. Package.json file. "scripts"{ "start":"node index.js" } index.js.


1 Answers

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

like image 179
Colonel Panic Avatar answered Oct 13 '22 08:10

Colonel Panic