5

What I want is open new tab having Powershell and running specific command (in my case "npm run local").

I was expecting this will work,

wt -w 0 -p "Windows Powershell" npm run local

but, it gives error,

[error 0x80070002 when launching `npm run local']


Is it possible and how?

*Note: I have done lot of research and also read official documentation, but I was not able to find a way.

Rajan
  • 180
  • 2
  • 10

2 Answers2

2

Running Get-Command npm shows that npm is actually npm.cmd, so a CMD interpreted script, not PowerShell. When run directly at the command-line, PowerShell is okay with leaving the extension off, but when passed through a Windows Terminal profile, the extension appears to be required:

wt -w 0 -d . -p "Windows PowerShell" npm.cmd init
wt -w 0 -d . -p "Command Prompt" npm.cmd init

(Edit: -d . added per your comment. Without that, as you pointed out, it will always default to the %userprofile% directory).

That said, it seems from your comment that your use-case is to keep the tab open after the process completes. In that case ...

wt -w 0 -d . -p "Windows PowerShell" cmd /k npm run local
wt -w 0 -d . -p "Command Prompt" cmd /k npm run local
NotTheDr01ds
  • 1,982
  • 1
  • 4
  • 14
2

I found the best/recommended solution from Windows Terminal developers.

Answer on GitHub

For both Command Prompt and Powershell.

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run local"

Only for Powershell.

wt --window 0 -p "Windows Powershell" -d "$pwd" powershell -noExit "npm run local"


The best use of this feature while running commands for development, in my case

For Powershell

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run startMongo"`;  split-pane --horizontal -p "Windows Powershell" -d . powershell -noExit "npm run gulp-watch"`;  new-tab -p "Windows Powershell" -d . powershell -noExit "npm run local"

For Command Prompt

wt --window 0 -p "Windows Powershell" -d . powershell -noExit "npm run startMongo";  split-pane --horizontal -p "Windows Powershell" -d . powershell -noExit "npm run gulp-watch";  new-tab -p "Windows Powershell" -d . powershell -noExit "npm run local"
Rajan
  • 180
  • 2
  • 10
  • 1
    Is the `-d .` really required? It seems that it uses the current working directory by default, but I just tried with an `npm init` (which executed in the current directory) rather than `npm run local`. – NotTheDr01ds Apr 20 '21 at 14:32
  • Note that for anyone using PowerShell Core, it seems that you will need the `-c` flag, as in `pwsh -c -NoExit "npm run local"`. I missed that in my original answer. – NotTheDr01ds Apr 20 '21 at 14:34
  • I see I was wrong on PowerShell not being able to parse the `.cmd` from the command line. That said, since it's a `.cmd` file anyway, wouldn't it be more "proper" (or perhaps "efficient", although efficiency isn't really a goal here) to pass it directly to `cmd.exe`. Passing it to `powershell.exe` just invokes PowerShell, which in turn invokes CMD anyway. Can't you just cut out the middleman? – NotTheDr01ds Apr 20 '21 at 14:39
  • 1
    Yes, you are right I can cut the middleman by directly parsing to cmd.exe. Another thing, I tried without `-d .` option, but it is not executed in current directory (It is executed in my user directory i.e. `C:\Users\Rajan`). I guess you just excuted `npm init` which created a new project in your user directory. Try to create a project on Desktop & see if it's working. – Rajan Apr 20 '21 at 18:10
  • 1
    Yes, you're right, too, of course! I started in ``C:\``, changed to `$env:userprofile`, and naively assumed that because it created it in the userprofile directory, it was using the current directory. Of course, that was a bad assumption. Any other directory fails without the `-d .`, as you said. – NotTheDr01ds Apr 20 '21 at 18:17
  • Also found that, if you add the extension (`npm.cmd`), it runs with your original "just the profile" style command. But that doesn't work if you want to keep the tab open when the process ends. – NotTheDr01ds Apr 20 '21 at 19:15