用于Windows的NPM包'bin'脚本

jbp*_*ros 37 windows posix package node.js npm

Cucumber.js提供了一个命令行"binary",它是一个.js包含shebang指令的简单文件:

#!/usr/bin/env node
var Cucumber = require('../lib/cucumber');
// ...
Run Code Online (Sandbox Code Playgroud)

package.json使用"bin"配置键指定二进制文件:

{ "name" : "cucumber"
, "description" : "The official JavaScript implementation of Cucumber."
// ...
, "bin": { "cucumber.js": "./bin/cucumber.js" }
// ...
Run Code Online (Sandbox Code Playgroud)

这一切都适用于POSIX系统.有人在Windows上运行Cucumber.js时报告了一个问题.

基本上,该.js文件似乎是通过Windows的JScript解释器(而不是Node.js)执行的,并且由于shebang指令而引发语法错误.

我的问题是:建议在UNIX和Windows系统上运行的"二进制"脚本的推荐方法是什么?

谢谢.

Col*_*nic 56

Windows忽略了shebang行#!/usr/bin/env node,并将根据.js文件关联执行它.明确用节点调用脚本

node hello.js
Run Code Online (Sandbox Code Playgroud)

PS.Pedantry:shebangs 符合POSIX标准,但大多数*nix系统都支持它们.


如果您将项目打包为Npm,请使用package.json中的"bin"字段.然后在Windows上,Npm将.cmd在脚本旁边安装一个包装器,以便用户可以从命令行执行它

hello
Run Code Online (Sandbox Code Playgroud)

对于npm来创建垫片,脚本必须有shebang线 #!/usr/bin/env node

  • 问题是二进制脚本名称以".js"后缀结尾.NPM根据"bin"配置指令创建一个对unix友好的`cucumber.js`和一个`cucumber.js.cmd` windows友好的二进制文件.由于Windows处理文件"扩展"的方式,当一个类型为`node_modules\.bin\cucumber.js`时,它通过JScript而不是`.cmd`文件运行`.js`文件.感谢迂腐的后记;) (4认同)

Tra*_*er1 6

你的"bin"应该是"cucumber"npm会创建一个指向"node%SCRIPTNAME%"的"cucumber"或"cucumber.cmd"文件.前者用于posix环境,后者用于windows使用...如果你想让"js"成为可执行文件名的一部分......你应该使用hyphon代替......"cucumber-js"......在您的情况下,.js文件将出现在.js.cmd之前,导致WScript解释器将其作为JScript文件而不是节点脚本运行.

我建议看看coffee-script的package.json就是一个很好的例子.

{
  "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"
  }
}
Run Code Online (Sandbox Code Playgroud)