【问题标题】:How do you run electronjs via npx instead of npm start?你如何通过 npx 而不是 npm start 运行 electronjs?
【发布时间】:2021-04-16 01:50:08
【问题描述】:

我正在尝试使用 electronjs 制作一个 npm 初始化程序。根据npm documentation,要进行初始化,您的脚本需要以npx 启动。

Electronjs 脚本 require that they be started with the electron 命令而不是 node 命令。问题是 npm init (npx) 在node 中启动命令。我看到electron-start 包以某种方式实现了这一点,但我不明白如何。

每当我尝试从 PowerShell(而不是电子)中的 node 命令执行 const electron = require( 'electron' ) 时,electron 都会返回可执行文件的路径字符串,而不是包含 BrowserWindowapp 属性的对象。

【问题讨论】:

  • 是第1行的shebang吗?我认为 shebangs 在 Windows 上不起作用

标签: javascript node.js electron npx npm-init


【解决方案1】:

好的,我明白了。它的文档记录很差,很难找到,但是我发现了导致 electron-start 在 npm's npx's cmd shim utility. 内的 windows 上工作的代码行:


var ...
  , shebangExpr = /^#\!\s*(?:\/usr\/bin\/env)?\s*([^ \t]+)(.*)$/

...

function writeShim (from, to, cb) {
  // make a cmd file and a sh script
  // First, check if the bin is a #! of some sort.
  // If not, then assume it's something that'll be compiled, or some other
  // sort of script, and just call it directly.
  mkdir(path.dirname(to), function (er) {
    if (er)
      return cb(er)
    fs.readFile(from, "utf8", function (er, data) {
      if (er) return writeShim_(from, to, null, null, cb)
      var firstLine = data.trim().split(/\r*\n/)[0]
        , shebang = firstLine.match(shebangExpr)           //<-- matched here
      if (!shebang) return writeShim_(from, to, null, null, cb)
      var prog = shebang[1]
        , args = shebang[2] || ""
      return writeShim_(from, to, prog, args, cb)
    })
  })
}

根据npx (package.json &gt; "bin") 上的 npm,shebang-ing node 可执行文件是“必需的”。这不是真的。虽然需要 shebang-ing,但可以在 Windows 上换出节点可执行文件。你可以在这里看到微光带any shebang (shim on wikipedia)

信用:https://stackoverflow.com/a/10398567/10534510

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 2023-02-13
    • 1970-01-01
    • 2021-02-15
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多