Home > database >  Open new terminal in MacOSX and run node script
Open new terminal in MacOSX and run node script

Time:05-27

I have been trying to figure out how to open a new terminal window from a Node application and run a preset command or script there.

I can open the terminal window:

const { exec } = require('child_process')

exec('open -a Terminal '   process.env.HOME)

And this works fine, I have done a bit of research but it seems I can only find examples directed to bash

Could anyone help me on this?

Thanks

CodePudding user response:

One way to do this would be to place the commands you want to run in a shell script with the .command extension. By default, these files will be opened in the Terminal application on macOS. This is a bit of a workaround, but I think it achieves what you would like to do.

Say we have the file hello.js containing the following:

console.log("Hello World")

And the shell script run_node.command

#!/bin/bash
node hello.js

Running the command open run_node.command will open a new Terminal.app instance and run the shell script, which in turn runs the node command. The terminal will close when the script exits.

  • Related