Search code examples
node.jsswiftnstask

Running node commands in Swift


I'm trying to run nodejs commands using Swift in my OS X app.

Running commands like echo work but when I'm trying to run node:

func node(args: String...) -> Int32 {
  let task = NSTask()
  task.launchPath = "/usr/bin/env"
  task.arguments = args
  task.launch()
  task.waitUntilExit()
  return task.terminationStatus
}

node("/usr/local/bin/myscript")
node("node")

node("/usr/local/bin/myscript") is a scriptinstalled via npm install -g myscript


Solution

  • Try:

    node("node", "/usr/local/bin/myscript")
    

    So that env starts the node program, with your script as a parameter to node.