【发布时间】:2018-08-07 07:30:54
【问题描述】:
我正在尝试使用 node 创建一个命令行工具(遵循these instructions)。
所以我将“bin”添加到 package.json 文件中
"bin": {
"say-hello": "./index.js"
}
创建 index.js 文件
#!/usr/bin/env node
console.log('Hello');
然后我做了链接
npm link
npm WARN part-a@1.0.0 No description
npm WARN part-a@1.0.0 No repository field.
up to date in 5.788s
found 0 vulnerabilities
C:\Users\ka20022276\AppData\Roaming\npm\say-hello ->
C:\Users\ka20022276\AppData\Roaming\npm\node_modules\part-a\index.js
C:\Users\ka20022276\AppData\Roaming\npm\node_modules\part-a ->
D:\Learning\NodeJS HandsOn\Assignment 2\Part a
但是如果我使用命令会抛出错误
D:\Learning\NodeJS HandsOn\Assignment 2\Part a>say-hello
'say-hello' is not recognized as an internal or external command,
operable program or batch file.
我错过了什么?
【问题讨论】:
-
我不这么认为。它创建一个
.cmd文件并忽略shebang 行 -
它在 windows 中出现,但对于 npm 是必需的。文章中有说明。 “在 Windows 中,该行将被忽略,因为它将被解释为注释,但它必须存在,因为 npm 会在安装 NodeJS 命令行包时在 Windows 机器上读取它。”
-
@KaranKulwal 尝试使用
npm i -g ./而不是npm linkmedium.com/@thatisuday/…