node.js 适用于 Windows 的 NPM 包“bin”脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10396305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:39:04  来源:igfitidea点击:

NPM package 'bin' script for Windows

windowsnode.jsposixpackagenpm

提问by jbpros

Cucumber.js is supplying a command-line "binary" which is a simple .jsfile containing a shebanginstruction:

Cucumber.js 提供了一个命令行“二进制”,它是一个.js包含shebang指令的简单文件:

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

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

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

{ "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 issuewhen running Cucumber.js on Windows.

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

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

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

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

我的问题是:设置适用于 UNIX 和 Windows 系统的“二进制”脚本的推荐方法是什么?

Thanks.

谢谢。

回答by Colonel Panic

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

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

node hello.js

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

附:Pedantry:shebangs不在POSIX 标准中,但大多数 *nix 系统都支持它们。



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

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

hello

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

为了让 npm 正确创建 shim,脚本必须有 shebang 行#!/usr/bin/env node

回答by Tracker1

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.

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

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

我建议查看咖啡脚本的 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"
  }
}