0

I am trying to make an npm initializer with electronjs. According to the npm documentation, to make an initializer, your script needs to be started with npx.

Electronjs scripts require that they be started with the electron command and not the node command. The problem is that npm init (npx) starts commands in node. I saw that the electron-start package somehow achieves this, but I don't understand how.

Whenever I try to const electron = require( 'electron' ) from the node command in PowerShell (instead of electron), electron returns a path string to the executable, and not an object containing the BrowserWindow or app properties.

DaMaxContent
  • 150
  • 1
  • 13

1 Answers1

0

Ok I got it. It's poorly documented and hard to find, however I found the line of code that causes electron-start to work on windows from within npm's npx's cmd shim utility.:


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)
    })
  })
}

According to npm on npx (package.json > "bin"), shebang-ing the node executable is "required." This is not true. While shebang-ing is required, the node executable can be swapped out on Windows. You can see here that the shimmer takes any shebang (shim on wikipedia).

Credit: https://stackoverflow.com/a/10398567/10534510

DaMaxContent
  • 150
  • 1
  • 13