In my extension I have to call functions depending on the host operating system, how do I get infos about host operating system ?
-
From briefly looking over the [VSCode API](https://code.visualstudio.com/api/references/vscode-api) it seems to be intended to be used as a cross-platform API, so ideally there should not be a need to make this kind of distinction. Are you missing any specific capability? – Boaz Sep 14 '20 at 10:56
-
It's related to my previous post https://stackoverflow.com/questions/50983314/vs-code-open-new-terminal-from-powershell/56439295#56439295 I use VS Extension to open terminal and send some shell commands. Some are slightly different between operating systems. – Yvain Sep 14 '20 at 12:05
3 Answers
On Windows
process.platform==='win32'
is true, and that is still the case on my 64-bit windows 11.
On MacOs
process.platform==='darwin'
On Linux Mint
process.platform==='linux'
On my wsl/ubuntu installation
process.platform==='linux'
(.. it would also be nice to distinguish wsl from a full linux os)
There is no need to import/require process
.

- 369
- 4
- 8
You can also use the os
package from node. See os.type:
const os = require('os');
or import
it
Returns the operating system name as returned by uname(3). For example, it returns
Linux
on Linux,Darwin
on macOS, andWindows_NT
on Windows.
I do not believe you need to install the os
package separately - it is made available to extension authors automatically.

- 143,421
- 24
- 428
- 436
-
Thank you, this is what works for me: const os - require('os'); os.platform() == 'win32' – Melvin Guerrero Jun 19 '22 at 05:19
This is what I did for my extension, it isn't the most robust solution but it works well enough for my purposes:
import { env } from 'vscode';
const isWindows = () => Boolean(env.appRoot && env.appRoot[0] !== "/");
The rationale is that all UNIX-like OS:es will have a root starting with /
, and a Windows-based install will have something like C:
.
If you don't mind having a hard-dependency on Node internals, you can use process.platform
instead.

- 1,607
- 10
- 23