【问题标题】:Unable to create "bin" command for NPM无法为 NPM 创建“bin”命令
【发布时间】:2017-11-27 21:59:32
【问题描述】:

我的包有以下配置:

{
    "name": "put-version",
    "version": "1.0.7",
    "description": "...",
    "main": "index.js",
    "scripts": {
        "test": "node index.js test.html",
        "compile": "babel src/index.js -o index.js",
        "start": "node index.js"
    },
    "engines": {
        "node": ">=4"
    },
    "bin": "index.js",
    ...
}

当人们安装这个包时,他们应该能够运行put-version。但是在 Windows 上运行时,该脚本似乎是由 Microsoft JScript 而不是 Node.js 执行的。 我已经读过这个:NPM package 'bin' script for Windows - 但我没有找到解决方案。

我正在运行带有 Node.js 8.9.1 和 NPM 5.5.1 的最新版本的 Windows 10。

【问题讨论】:

  • 不知道为什么要标记 javascript
  • 看起来Node没有配置为运行JS文件的默认应用程序然后...
  • @DeepKakkar 它与 JavaScript 开发有关。不是吗?
  • @James 但在其他情况下它确实有效。我已经测试过 opn-cli (github.com/sindresorhus/opn-cli)。我还查看了它的package.json,但看起来都非常相似。
  • @LarsKliesing 有一个区别,它有一个opn 的子键,听起来你可能需要做同样的事情,例如"bin": { "put-version: "./index.js" }。 FWIW 如果您的包是 CLI 包,通常约定在名称前加上 cli

标签: javascript node.js windows npm


【解决方案1】:

我找到了解决方案: 我不得不把它放在脚本的顶部(index.js):

#!/usr/bin/env node

所以文件的开头是这样的:

#!/usr/bin/env node
"use strict";

var fs = require("fs");
var path = require("path");
var cheerio = require("cheerio");
// ...

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 2017-01-11
    • 1970-01-01
    • 2019-11-08
    • 2014-04-16
    • 1970-01-01
    • 2016-06-03
    • 2017-02-20
    • 2017-09-14
    相关资源
    最近更新 更多