适用于 Windows 的 NPM 包“bin"脚本 [英] NPM package 'bin' script for Windows
问题描述
Cucumber.js 提供了一个命令行二进制",它是一个包含 shebang 指令的简单 .js
文件:
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');
// ...
二进制文件在 package.json
中使用 "bin"
配置键指定:
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" }
// ...
这一切都适用于 POSIX 系统.有人报告了在 Windows 上运行 Cucumber.js 时一个问题.
This all works well on POSIX systems. Someone reported an issue when running Cucumber.js on Windows.
基本上,.js
文件似乎是通过 Windows 的 JScript 解释器(不是 Node.js)执行的,并且由于 shebang 指令而引发语法错误.
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.
我的问题是:在 UNIX 和 Windows 系统上设置二进制"脚本的推荐方法是什么?
My question is: what is the recommended way of setting up a "binary" script that works on both UNIX and Windows systems?
谢谢.
推荐答案
Windows 忽略 shebang 行 #!/usr/bin/env node
并按照 .js 执行
文件关联.明确使用节点调用脚本
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 不在 POSIX 标准中,但大多数 *nix 系统都支持它们.
ps. Pedantry: shebangs aren't in the POSIX standard but they are supported by most *nix system.
如果您为 Npm 打包项目,请使用 package.json 中的bin"字段.然后在 Windows 上,Npm 会在你的脚本旁边安装一个 .cmd
包装器,以便用户可以从命令行执行它
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
要让 npm 正确创建 shim,脚本必须shebang 行 #!/usr/bin/env node
For npm to create the shim right, the script must have the shebang line #!/usr/bin/env node
这篇关于适用于 Windows 的 NPM 包“bin"脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!