@vltpkg/run

Run a script defined in a package.json file (eg, vlt run and
lifecycle scripts), or an arbitrary command as if it was (eg,
vlt exec).
Usage
import { run, exec } from '@vltpkg/run'
const cwd = '/path/to/pkg'
const runResult = await run({
event: 'build',
cwd,
ignoreMissing: true,
args: ['some', 'arguments'],
env: process.env,
foreground: true,
'script-shell': '/usr/bin/bash',
packageJson: new PackageJson(),
})
const execResult = await exec({
command: 'do-something',
args: ['some', 'arguments'],
})
Node-gyp Shimming
The @vltpkg/run package automatically provides node-gyp shimming for
commands that contain node-gyp references. This allows packages that
expect node-gyp to be available to work seamlessly with vlt's
package management system.
How it works
When executing commands that contain references to node-gyp, the
package will:
- Create a
node-gyp shim file in the XDG runtime directory
(typically ~/.run/vlt/run/node-gyp on Unix or
%TEMP%\xdg.run\vlt\run\node-gyp.cmd on Windows)
- Inject the shim directory into the command's
PATH environment
variable
- The shim redirects all
node-gyp calls to vlx node-gyp@latest
The shim is created once per session and cached in memory for
performance. It works for both simple commands like node-gyp rebuild
and complex commands with shell operators like
echo "before" && node-gyp rebuild && echo "after".
Cross-platform support
The shimming system is fully cross-platform:
- Unix/Linux/macOS: Creates an executable shell script with
shebang (
#!/bin/sh)
- Windows: Creates a batch file (
.cmd) that forwards arguments
Examples
await run({
cwd: '/path/to/pkg',
arg0: 'build',
projectRoot: '/path/to/pkg',
})
await exec({
arg0: 'echo "before" && node-gyp rebuild && echo "after"',
cwd: '/path/to/pkg',
projectRoot: '/path/to/pkg',
'script-shell': true,
})
await exec({
arg0: 'node-gyp',
args: ['configure', '--debug'],
cwd: '/path/to/pkg',
projectRoot: '/path/to/pkg',
})
await exec({
arg0: 'echo "hello"',
cwd: '/path/to/pkg',
projectRoot: '/path/to/pkg',
})
How to verify the shim
You can access the shim utilities to inspect or verify the setup:
import {
getNodeGypShim,
getNodeGypShimDir,
hasNodeGypReference,
} from '@vltpkg/run'
const shimPath = await getNodeGypShim()
const shimDir = await getNodeGypShimDir()
const needsShim = hasNodeGypReference('node-gyp rebuild')
Fallback behavior
If the shim cannot be created (e.g., due to filesystem permissions),
the error is silently caught and the command executes normally. This
ensures the command fails naturally if node-gyp is actually needed
but not available, providing clear error messages to the user.