npm 生成的命令不调用节点 [英] npm-generated command not calling node
问题描述
我正在尝试创建一个 node.js 命令行实用程序,但 npm 生成的包装器存在问题.我的超简单演示是一个 package.json 文件:
I'm trying to create a node.js command line utility and having an issue with wrapper that npm generates. My super-simple demo is a package.json file:
{
"name": "demo-cli",
"version": "0.0.1",
"bin": { "demo": "bin/demo-cli.js" }
}
以及 demo-cli.js 文件:
And the demo-cli.js file:
console.log('DEMO WORKED');
问题是当我使用 npm install -g
安装模块时,在项目目录中,它生成的包装器不会以文件作为参数调用 node.这是demo.cmd生成的内容:
The issue is when I install the module using npm install -g
while in the project directory the wrapper it generates does not call node with the file as the parameter. This is the generated contents of demo.cmd:
:: Created by npm, please don't edit manually.
"%~dp0\.\node_modules\demo-cli\bin\demo-cli.js" %*
unix 版本也有同样的问题.查看另一个全局安装的 CLI,我可以看到该节点正在按预期进行测试和调用.
The unix version has the same issue. Looking at another globally installed CLI I can see that node is being tested for and called as expected.
:: Created by npm, please don't edit manually.
@IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\.\node_modules\mocha\bin\mocha" %*
) ELSE (
node "%~dp0\.\node_modules\mocha\bin\mocha" %*
)
npm 生成正确的脚本文件所需的秘诀是什么?我已经尝试了我能找到或想到的所有方法,包括删除 .js 扩展名、更喜欢全局、指定节点版本......没有运气.
What is the secret sauce needed for npm to generate the proper script file? I've tried everything I could find or think of, including removing the .js extension, preferring global, specifying the node version...no luck.
我在 Windows 7 x64 上使用 node v0.6.18 和 npm v1.1.21.
I'm using node v0.6.18 and npm v1.1.21 on Windows 7 x64.
推荐答案
尝试放置
#!/usr/bin/env node
在 demo-cli.js
文件的顶部.
在 Windows 上,npm
在创建 .cmd
包装器时会查找此 shebang 行.请参阅cmd-shim.jscode>npm 源代码以获取更多信息.
On windows, npm
looks for this shebang line when creating the .cmd
wrapper. See cmd-shim.js in the npm
source for more info.
这篇关于npm 生成的命令不调用节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!