debugging.md
This file
Path
docs/debugging.md
105 lines · 3.3 KiB · Markdown
Last changed by
Add user guides (#989)ehmickybefore adoptionbe91ecf
History of this fileTake it
debugging.md
🐛 Debugging
Command
error.command contains the file and arguments that were run. It is intended for logging or debugging.
error.escapedCommand is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes.
Since the escaping is fairly basic, neither error.command nor error.escapedCommand should be executed directly, including using execa() or execaCommand().
import {execa} from 'execa';
try {
await execa`npm run build\ntask`;
} catch (error) {
console.error(error.command); // "npm run build\ntask"
console.error(error.escapedCommand); // "npm run 'build\\ntask'"
throw error;
}
Duration
try {
const result = await execa`npm run build`;
console.log('Command duration:', result.durationMs); // 150
} catch (error) {
console.error('Command duration:', error.durationMs); // 150
throw error;
}
Verbose mode
Short mode
When the verbose option is 'short', the command, duration and error messages are printed on stderr.
// build.js
await execa({verbose: 'short'})`npm run build`;
$ node build.js
[20:36:11.043] [0] $ npm run build
[20:36:11.885] [0] ✔ (done in 842ms)
Full mode
When the verbose option is 'full', the subprocess' stdout and stderr are also logged. Both are printed on stderr.
The output is not logged if either:
- The
stdout/stderroption is'ignore'or'inherit'. - The
stdout/stderris redirected to a stream, a file, a file descriptor, or another subprocess. - The
encodingoption is binary.
// build.js
await execa({verbose: 'full'})`npm run build`;
$ node build.js
[20:36:11.043] [0] $ npm run build
Building application...
Done building.
[20:36:11.885] [0] ✔ (done in 842ms)
Global mode
When the NODE_DEBUG=execa environment variable is set, the verbose option defaults to 'full' for all commands.
// build.js
// This is logged by default
await execa`npm run build`;
// This is not logged
await execa({verbose: 'none'})`npm run test`;
$ NODE_DEBUG=execa node build.js
[20:36:11.043] [0] $ npm run build
Building application...
Done building.
[20:36:11.885] [0] ✔ (done in 842ms)
Next: 📎 Windows
Previous: 📞 Inter-process communication
Top: Table of contents