Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to suppress NPM's echo of the commands it is running?

I've got a bash script that starts up a server and then runs some functional tests. It's got to happen in one script, so I'm running the server in the background. This all happens via 2 npm commands: start:nolog and test:functional.

All good. But there's a lot of cruft in the output that I don't care about:

✗ ./functional-tests/runInPipeline.sh

(... "good" output here)

> @co/[email protected] pretest:functional /Users/jcol53/Documents/work/foo
> curl 'http://localhost:3000/foo' -s -f -o /dev/null || (echo 'Website must be running locally for functional tests.' && exit 1)


> @co/[email protected] test:functional /Users/jcol53/Documents/work/foo
> npm run --prefix functional-tests test:dev:chromeff


> @co/[email protected] test:dev:chromeff /Users/jcol53/Documents/work/foo/functional-tests
> testcafe chrome:headless,firefox:headless ./tests/**.test.js  -r junit:reports/functional-test.junit.xml -r html:reports/functional-test.html --skip-js-errors

That's a lot of lines that I don't need there. Can I suppress the @co/foo-functional-tests etc lines? They aren't telling me anything worthwhile...

npm run -s kills all output from the command, which is not what I'm looking for.

This is probably not possible but that's OK, I'm curious, maybe I missed something...

like image 400
jcollum Avatar asked Jan 30 '26 01:01

jcollum


1 Answers

I have reproduced this issue with a repository on github using Github Actions for observation.

The npm silent option -s removes verbose text except its own output.

However this option only works with direct passing.

Solution 1. Pass -s directly

Let's consider the sample script with npm

here are two scripts.

#!/bin/bash
npm pkg set scripts.myecho="echo sample"
npm pkg set scripts.silent_myecho="npm run -s myecho"

The results are as follows:

$ npm run myecho # verbose

> [email protected] myecho
> echo sample

sample

$ npm run silent_myecho  # verbose

> [email protected] silent_myecho
> npm run -s myecho

sample

$ npm run -s myecho # no verbose
sample

$ npm run -s silent_myecho # no verbose
sample

You can find the same results on the reproduced Github Action Job.

Solution 2. Filter with grep invert option

Alternatively, you can filter npm outputs using grep with the invert option

npm run myecho | grep -v "^>" # a carrot is used as regex for matching

then

npm run myecho | grep -iv "^>"

Or

npm run myecho | grep -v "^>" | tr -d '\n'

This approach might be useful when you need to extract output but cannot use the silent option, such as with npm test.

like image 190
cyan-kinesin Avatar answered Feb 01 '26 16:02

cyan-kinesin