You need to trim the string, as it has invisible whitespace at the end in the form of a newline.
const exec = require('child_process').exec;
exec('docker inspect -f {{.State.Running}} service-redis', (err, stdout, stderr) => {
// here stdout has value "true"
console.log(stdout.trimRight() === 'true')
});
Note that the trailing newline is not Node doing anything weird. It is generally the case that programs append a newline to their console output, particularly if a human will be looking at it. In fact, this is one of the things console.log()
does for you.
Imagine using your terminal if the programs you use every day (ls
, git
, ...) terminated their output without a newline. Your prompt (the marker before where you type, e.g. $
) would be crowded on the same line as the output from the last program. Yuck!
Even files usually end with a newline, as this makes it easier to concatenate data together from various sources (e.g. log files) and work with UNIX tools. There is also a widely held practice to have your code editor enforce this for you. In short, you will see this all over the place.
The childProcess.exec()
method does not make any assumptions about these newline conventions, it is too low-level for that, and so it keeps them in place. Issues of tiny modules aside, you could easily create an abstraction on top of exec()
that handles trimming for you.