Google’s open source project zx enables efficient execution of external commands like Bash, Powershell, and Command Prompt in node.js through natural embedding in mjs scripts. Below we introduce the installation and usage of zx.

Github:
google/zx: A tool for writing better scripts
Install nvm (node.js version management system) using chocolatey. This allows you to install any version of node.js in a later step.
choco install nvm -y
refreshenv
For an overview of what chocolatey is and how to install it, see the following.
Windows 11にchocolateyをインストールする
Use the installed nvm to install node.js version 14.13.1 with the following command. node.js v14.13.1 is one of the requirements specified as a necessary environment for Google/xz. Install v14.13.1 or higher of node.js.
nvm install 14.13.1
Verify that node.js is installed.
> C:\ProgramData\nvm\v14.13.1\node64.exe -v
v14.13.1
Create and move to an arbitrary directory. Initialize the node.js project with the npm init command to create a package.json file, and install the zx library with the npm install command.
cd C:\pg\node\_win_v14.13.1_dev\
C:\ProgramData\nvm\v14.13.1\npm.cmd init
C:\ProgramData\nvm\v14.13.1\npm.cmd install [email protected]
Below is a sample code example of a node.js program using zx.
If executing as a .mjs file:
// tmp.mjs
import 'zx/globals'
await $`pwd`
To execute the above .mjs file, run it as a node.js program with the following command:
./node_modules/.bin/zx .\tmp.mjs
# or
node .\tmp.mjs
# or
C:\ProgramData\nvm\v14.13.1\node64.exe .\tmp.mjs
If executing as a .js file:
// tmp.js
const zx = require('zx/globals');
(async function(){
await $`pwd`;
})();
To execute the above .js file:
node .\tmp.js
Each of the above programs is the simplest example that can be used cross-platform, executing the “pwd” command as an external command and getting the output.
The “pwd” command outputs the directory path of the current working directory. The result of this output depends on what environment and operating system the node.js program is running on, so it can’t be said definitively what it will be, but in my environment it’s running in Windows Powershell and WSL Bash environments, so it produces output like the following:
$ pwd
/mnt/c/pg/node/_win_v14.13.1_dev
Note that the dependencies of my execution environment and the vscode launch.json settings are as follows:
(package.json)
{
"name": "win_v14.13.1_dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"nodemon": "^2.0.15",
"zx": "^4.2.0"
}
}
(launch.json)
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "nodejs",
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/nodemon",
"runtimeArgs": [
"--ext",
"js,mjs",
"--exec",
"C:/ProgramData/nvm/v14.13.1/node64.exe ${file}"
],
"restart": true
}
]
}